{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0cee3b92",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=128 lang=java\n",
        " *\n",
        " * [128] 最长连续序列\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int longestConsecutive(int[] nums) {\n",
        "        Set<Integer> num_set = new HashSet<Integer>();\n",
        "        for (int num : nums) {\n",
        "            num_set.add(num);\n",
        "        }\n",
        "\n",
        "        int longestStreak = 0;\n",
        "\n",
        "        for (int num : num_set) {\n",
        "            if (!num_set.contains(num - 1)) {\n",
        "                int currentNum = num;\n",
        "                int currentStreak = 1;\n",
        "\n",
        "                while (num_set.contains(currentNum + 1)) {\n",
        "                    currentNum += 1;\n",
        "                    currentStreak += 1;\n",
        "                }\n",
        "\n",
        "                longestStreak = Math.max(longestStreak, currentStreak);\n",
        "            }\n",
        "        }\n",
        "\n",
        "        return longestStreak;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bddf9bd9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=334 lang=java\n",
        " *\n",
        " * [334] 递增的三元子序列\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean increasingTriplet(int[] nums) {\n",
        "        int n = nums.length;\n",
        "        if(n < 3){\n",
        "            return false;\n",
        "        }\n",
        "         // leftMin[i] 表示 Math.min(nums[0...i])\n",
        "        int[] leftMin = new int[n];\n",
        "        leftMin[0] =nums[0];\n",
        "        for(int i =1;i<n;i++){\n",
        "            leftMin[i] = Math.min(leftMin[i-1],nums[i]);\n",
        "        }\n",
        "        // rightMax[i] 表示 Math.max(nums[i..n-1])\n",
        "        int[] rightMax = new int[n];\n",
        "        rightMax[n-1] = nums[n-1];\n",
        "        for(int i = n-2;i>=0;i--){\n",
        "            rightMax[i] = Math.max(rightMax[i+1],nums[i]);\n",
        "        }\n",
        "\n",
        "        for(int i = 1;i<n-1;i++){\n",
        "            if(nums[i] > leftMin[i-1] && nums[i] < rightMax[i+1]){\n",
        "                return true;\n",
        "            }\n",
        "        }\n",
        "        return false;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ced224ca",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=637 lang=java\n",
        " *\n",
        " * [637] 二叉树的层平均值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    List<Double> avgs = new ArrayList<>();\n",
        "    public List<Double> averageOfLevels(TreeNode root) {\n",
        "\n",
        "        bts(root);\n",
        "        return avgs;\n",
        "\n",
        "    }\n",
        "\n",
        "    void bts(TreeNode root){\n",
        "        if(root == null) return;\n",
        "\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "        while(!q.isEmpty()){\n",
        "            int sz = q.size();\n",
        "            double sum =0;\n",
        "            for(int i =0;i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                sum += cur.val;\n",
        "\n",
        "                if(cur.left!=null) q.offer(cur.left);\n",
        "                if(cur.right!=null) q.offer(cur.right);\n",
        "\n",
        "            }\n",
        "            double avg = sum/sz;\n",
        "            avgs.add(avg);\n",
        "        }\n",
        "        return;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4ed4d0e3",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=394 lang=java\n",
        " *\n",
        " * [394] 字符串解码\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String decodeString(String s) {\n",
        "\n",
        "\n",
        "        Stack<Character> stack = new Stack<>();\n",
        "\n",
        "        for(char c :s.toCharArray()){\n",
        "            if(c !=']'){\n",
        "                stack.push(c);\n",
        "            }else{\n",
        "\n",
        "                //Step 1: 取出[]内的字符串\n",
        "                StringBuilder sb = new StringBuilder();\n",
        "                while(!stack.isEmpty() && Character.isLetter(stack.peek())){\n",
        "                    sb.insert(0,stack.pop());\n",
        "                }\n",
        "                //[] 内的字符串\n",
        "                String sub = sb.toString();\n",
        "                //去除 [\n",
        "                stack.pop();\n",
        "\n",
        "                //Step2:获取倍数数字\n",
        "                sb = new StringBuilder();\n",
        "                while(!stack.isEmpty()&&Character.isDigit(stack.peek())){\n",
        "                    sb.insert(0,stack.pop());\n",
        "                } \n",
        "                //倍数\n",
        "                int count  =Integer.valueOf(sb.toString());\n",
        "                //Step3:根据倍数把字母再push 回去\n",
        "                while(count > 0 ){\n",
        "                    for(char ch : sub.toCharArray()){\n",
        "                        stack.push(ch);\n",
        "                    }\n",
        "                    count -- ;\n",
        "                }\n",
        "            }\n",
        "\n",
        "        }\n",
        "        // 把栈里面所有的字母取出来，完事\n",
        "        StringBuilder retv = new StringBuilder();\n",
        "        while(!stack.isEmpty()){\n",
        "            retv.insert(0,stack.pop());\n",
        "        }\n",
        "        return retv.toString();\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "14ff4eb6",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=54 lang=java\n",
        " *\n",
        " * [54] 螺旋矩阵\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public  List<Integer> spiralOrder(int[][] matrix) {\n",
        "        LinkedList<Integer> result = new LinkedList<>();\n",
        "        if(matrix==null||matrix.length==0) return result;\n",
        "        int left = 0;\n",
        "        int right = matrix[0].length - 1;\n",
        "        int top = 0;\n",
        "        int bottom = matrix.length - 1;\n",
        "        int numEle = matrix.length * matrix[0].length;\n",
        "        while (numEle >= 1) {\n",
        "            for (int i = left; i <= right && numEle >= 1; i++) {\n",
        "                result.add(matrix[top][i]);\n",
        "                numEle--;\n",
        "            }\n",
        "            top++;\n",
        "            for (int i = top; i <= bottom && numEle >= 1; i++) {\n",
        "                result.add(matrix[i][right]);\n",
        "                numEle--;\n",
        "            }\n",
        "            right--;\n",
        "            for (int i = right; i >= left && numEle >= 1; i--) {\n",
        "                result.add(matrix[bottom][i]);\n",
        "                numEle--;\n",
        "            }\n",
        "            bottom--;\n",
        "            for (int i = bottom; i >= top && numEle >= 1; i--) {\n",
        "                result.add(matrix[i][left]);\n",
        "                numEle--;\n",
        "            }\n",
        "            left++;\n",
        "        }\n",
        "        return result;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ffaba844",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=39 lang=java\n",
        " *\n",
        " * [39] 组合总和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<Integer>> combinationSum(int[] candidates, int target) {\n",
        "        List<List<Integer>> ans = new ArrayList<List<Integer>>();\n",
        "        List<Integer> combine = new ArrayList<Integer>();\n",
        "        dfs(candidates,target,ans,combine,0);\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public void dfs(int[] candidates,int target,List<List<Integer>> ans,\n",
        "    List<Integer> combine,int idx){\n",
        "        if(idx == candidates.length){\n",
        "            return;\n",
        "        }\n",
        "        if(target == 0){\n",
        "            ans.add(new ArrayList<Integer>(combine));\n",
        "            return ;\n",
        "        }\n",
        "\n",
        "        //直接跳过\n",
        "        dfs(candidates,target,ans,combine,idx+1);\n",
        "        //选择当前数\n",
        "        if(target -candidates[idx] >= 0){\n",
        "            combine.add(candidates[idx]);\n",
        "            dfs(candidates,target-candidates[idx],ans,combine,idx);\n",
        "            combine.remove(combine.size()-1);\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cb918420",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=513 lang=java\n",
        " *\n",
        " * [513] 找树左下角的值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    TreeNode leftLeaf = null;\n",
        "    public int findBottomLeftValue(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return leftLeaf.val;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "\n",
        "        while(!q.isEmpty()){\n",
        "            int size = q.size();\n",
        "            for(int i=0;i<size;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                if(i==0){\n",
        "                    leftLeaf = cur;\n",
        "                }\n",
        "                if(cur.left != null){\n",
        "                    q.offer(cur.left);\n",
        "                }\n",
        "                if(cur.right != null){\n",
        "                     q.offer(cur.right);\n",
        "                }\n",
        "            }\n",
        "           \n",
        "            \n",
        "           \n",
        "        }\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "77370c2c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=33 lang=java\n",
        " *\n",
        " * [33] 搜索旋转排序数组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int search(int[] nums, int target) {\n",
        "\n",
        "        int n = nums.length;\n",
        "        if(n == 0){return -1;}\n",
        "        if(n==1){return nums[0] == target ? 0: -1;}\n",
        "        int l =0, r = n -1;\n",
        "        while(l <= r){\n",
        "            int mid = (l+r) /2;\n",
        "            if(nums[mid] == target){\n",
        "                return mid;\n",
        "            }\n",
        "            if(nums[0] <= nums[mid]){\n",
        "                if(nums[0] <= target && target < nums[mid]){\n",
        "                    r = mid -1;\n",
        "                }else{\n",
        "                    l = mid +1;\n",
        "                }\n",
        "            }else{\n",
        "                if(nums[mid] < target && target <= nums[n-1]){\n",
        "                    l = mid + 1;\n",
        "                }else{\n",
        "                    r = mid -1;\n",
        "                }\n",
        "            }\n",
        "        } \n",
        "        return -1;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "54292f72",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=155 lang=java\n",
        " *\n",
        " * [155] 最小栈\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class MinStack {\n",
        "    Deque<Integer> xStack;\n",
        "    Deque<Integer> minStack;\n",
        "\n",
        "    public MinStack() {\n",
        "        xStack = new LinkedList<Integer>();\n",
        "        minStack = new LinkedList<Integer>();\n",
        "        minStack.push(Integer.MAX_VALUE);\n",
        "    }\n",
        "    \n",
        "    public void push(int val) {\n",
        "        xStack.push(val);\n",
        "        minStack.push(Math.min(minStack.peek(),val));\n",
        "    }\n",
        "    \n",
        "    public void pop() {\n",
        "        xStack.pop();\n",
        "        minStack.pop();\n",
        "    }\n",
        "    \n",
        "    public int top() {\n",
        "        return xStack.peek();\n",
        "    }\n",
        "    \n",
        "    public int getMin() {   \n",
        "        return minStack.peek();\n",
        "    }\n",
        "}\n",
        "\n",
        "\n",
        "/**\n",
        " * Your MinStack object will be instantiated and called as such:\n",
        " * MinStack obj = new MinStack();\n",
        " * obj.push(val);\n",
        " * obj.pop();\n",
        " * int param_3 = obj.top();\n",
        " * int param_4 = obj.getMin();\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4fde54c9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1493 lang=java\n",
        " *\n",
        " * [1493] 删掉一个元素以后全为 1 的最长子数组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int longestSubarray(int[] nums) {\n",
        "        int n = nums.length;\n",
        "        int left =0,lsum =0,rsum=0;\n",
        "        int ans =0;\n",
        "        for(int right = 0;right<n;++right){\n",
        "            rsum += 1-nums[right];\n",
        "            while(lsum < rsum-1){\n",
        "                lsum += 1- nums[left];\n",
        "                left++;\n",
        "            }\n",
        "            ans = Math.max(ans,right-left +1);\n",
        "        }\n",
        "        return ans-1;\n",
        "    }\n",
        "    \n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f76dee5b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1261 lang=java\n",
        " *\n",
        " * [1261] 在受污染的二叉树中查找元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class FindElements {\n",
        "    TreeNode root = null;\n",
        "\n",
        "    public FindElements(TreeNode root) {\n",
        "        this.root = root;\n",
        "        if(this.root == null) return ;\n",
        "        root.val = 0;\n",
        "        dfs(this.root);\n",
        "\n",
        "\n",
        "    }\n",
        "    \n",
        "    public boolean find(int target) {\n",
        "        return find(this.root,target);\n",
        "\n",
        "    }\n",
        "    public boolean find(TreeNode root, int target) {\n",
        "        if(root == null) return false;\n",
        "        if(root.val == target) return true;\n",
        "        return find(root.left,target) ||find(root.right,target);\n",
        "    }\n",
        "\n",
        "    void dfs(TreeNode root){\n",
        "        if(root == null) return;\n",
        "        if(root.left!=null){\n",
        "            root.left.val = 2 * root.val +1;\n",
        "            dfs(root.left);\n",
        "        }\n",
        "         if(root.right!=null){\n",
        "            root.right.val = 2 * root.val +2;\n",
        "            dfs(root.right);\n",
        "        }\n",
        "    }\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your FindElements object will be instantiated and called as such:\n",
        " * FindElements obj = new FindElements(root);\n",
        " * boolean param_1 = obj.find(target);\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9bd3b094",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=876 lang=java\n",
        " *\n",
        " * [876] 链表的中间结点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode middleNode(ListNode head) {\n",
        "        ListNode slow = head, fast = head;\n",
        "        while(fast!=null&&fast.next!=null){\n",
        "            slow =slow.next;\n",
        "            fast = fast.next.next;\n",
        "        }\n",
        "        return slow;\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f6cb3d31",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=508 lang=java\n",
        " *\n",
        " * [508] 出现次数最多的子树元素和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    Map<Integer, Integer> cnt = new HashMap<Integer, Integer>();\n",
        "    int maxCnt = 0;\n",
        "\n",
        "    public int[] findFrequentTreeSum(TreeNode root) {\n",
        "        dfs(root);\n",
        "        List<Integer> list = new ArrayList<Integer>();\n",
        "        for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) {\n",
        "            int s = entry.getKey(), c = entry.getValue();\n",
        "            if (c == maxCnt) {\n",
        "                list.add(s);\n",
        "            }\n",
        "        }\n",
        "        int[] ans = new int[list.size()];\n",
        "        for (int i = 0; i < list.size(); ++i) {\n",
        "            ans[i] = list.get(i);\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public int dfs(TreeNode node) {\n",
        "        if (node == null) {\n",
        "            return 0;\n",
        "        }\n",
        "        \n",
        "        int sum = node.val + dfs(node.left) + dfs(node.right);\n",
        "        cnt.put(sum, cnt.getOrDefault(sum, 0) + 1);\n",
        "        maxCnt = Math.max(maxCnt, cnt.get(sum));\n",
        "        return sum;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "064f379d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=897 lang=java\n",
        " *\n",
        " * [897] 递增顺序搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    TreeNode rt = null;\n",
        "    TreeNode head = null;\n",
        "    public TreeNode increasingBST(TreeNode root) {\n",
        "            traverse(root);\n",
        "            return head;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;;\n",
        "\n",
        "        traverse(root.left);\n",
        "        if(rt == null){\n",
        "            rt = new TreeNode(root.val);\n",
        "            head = rt;\n",
        "        }else{\n",
        "            rt.right = new TreeNode(root.val);\n",
        "            rt = rt.right;\n",
        "        }\n",
        "        traverse(root.right);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4919f512",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=46 lang=java\n",
        " *\n",
        " * [46] 全排列\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<Integer>> permute(int[] nums) {\n",
        "        List<List<Integer>> res = new ArrayList<>();\n",
        "        Deque<Integer> list = new ArrayDeque<>();\n",
        "        dfs(res,nums,list);\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    public void dfs(List<List<Integer>> res,int[] nums,Deque<Integer> list){\n",
        "        if(list.size() == nums.length){\n",
        "            res.add(new ArrayList<>(list));\n",
        "            return;\n",
        "        }\n",
        "        for(int i =0;i<nums.length;i++){\n",
        "            if(list.contains(nums[i])){\n",
        "                continue;\n",
        "            }\n",
        "            list.addLast(nums[i]);\n",
        "            dfs(res,nums,list);\n",
        "            list.removeLast();\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "10500e80",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=48 lang=java\n",
        " *\n",
        " * [48] 旋转图像\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public void rotate(int[][] matrix) {\n",
        "        int n =matrix.length;\n",
        "        for(int i=0;i< n/2;++i){\n",
        "            for(int j=0;j<(n+1)/2;++j){\n",
        "                int temp = matrix[i][j];\n",
        "                matrix[i][j] = matrix[n-j-1][i];\n",
        "                matrix[n-j-1][i]=matrix[n-i-1][n-j-1];\n",
        "                matrix[n-i-1][n-j-1] = matrix[j][n-i-1];\n",
        "                matrix[j][n-i-1] =temp;\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "35d26a5a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=701 lang=java\n",
        " *\n",
        " * [701] 二叉搜索树中的插入操作\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode insertIntoBST(TreeNode root, int val) {\n",
        "        if(root == null) return new TreeNode(val);\n",
        "        if(root.val < val){\n",
        "            root.right = insertIntoBST(root.right,val);\n",
        "        }\n",
        "        if(root.val > val){\n",
        "            root.left = insertIntoBST(root.left,val);\n",
        "        }\n",
        "        return root;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "243ebb7b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=649 lang=java\n",
        " *\n",
        " * [649] Dota2 参议院\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String predictPartyVictory(String senate) {\n",
        "      int n = senate.length();\n",
        "      Queue<Integer> radiant = new LinkedList<Integer>();\n",
        "      Queue<Integer> dire = new LinkedList<Integer>();\n",
        "      for(int i=0;i<n;i++){\n",
        "          if(senate.charAt(i) == 'R'){\n",
        "              radiant.offer(i);\n",
        "          }else{\n",
        "              dire.offer(i);\n",
        "          }\n",
        "      }\n",
        "\n",
        "      while(!radiant.isEmpty() && !dire.isEmpty()){\n",
        "          int radiantIndex = radiant.poll(),direIndex = dire.poll();\n",
        "          if(radiantIndex < direIndex){\n",
        "              radiant.offer(radiantIndex +n);\n",
        "          }else{\n",
        "              dire.offer(direIndex+n);\n",
        "          }\n",
        "      }\n",
        "      return !radiant.isEmpty()?\"Radiant\":\"Dire\";\n",
        "\n",
        "\n",
        "    }\n",
        "\n",
        "   \n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1a24c55f",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=886 lang=java\n",
        " *\n",
        " * [886] 可能的二分法\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    private boolean ok = true;\n",
        "    private boolean[] color;\n",
        "    private boolean[] visited;\n",
        "    \n",
        "    public boolean possibleBipartition(int n, int[][] dislikes) {\n",
        "        color = new boolean[n+1];\n",
        "        visited = new boolean[n+1];\n",
        "\n",
        "        List<Integer>[] graph =buildGraph(n,dislikes);\n",
        "\n",
        "        for(int v =1; v<=n;v++){\n",
        "            if(!visited[v]){\n",
        "                traverse(graph,v);\n",
        "            }\n",
        "        }\n",
        "        return ok;\n",
        "\n",
        "    }\n",
        "    //建图\n",
        "    private List<Integer>[] buildGraph(int n,int[][] dislikes){\n",
        "        List<Integer>[] graph = new LinkedList[n+1];\n",
        "        for(int i =1;i<=n;i++){\n",
        "            graph[i] = new LinkedList<>();\n",
        "        }\n",
        "        for(int[] edge:dislikes){\n",
        "            int v =edge[1];\n",
        "            int w =edge[0];\n",
        "            //无向图 相当于 双向图\n",
        "            //v -->w\n",
        "            graph[v].add(w);\n",
        "            graph[w].add(v);\n",
        "        }\n",
        "        return graph;\n",
        "    }\n",
        "    private void traverse(List<Integer>[] graph,int v){\n",
        "        if(!ok) return ;\n",
        "        visited[v] =true;\n",
        "\n",
        "        for(int w :graph[v]){\n",
        "            if(!visited[w]){\n",
        "                color[w] =!color[v];\n",
        "                traverse(graph,w);\n",
        "            }else{\n",
        "                if(color[w] == color[v]){\n",
        "                    ok = false;\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7e1c4403",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=347 lang=java\n",
        " *\n",
        " * [347] 前 K 个高频元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] topKFrequent(int[] nums, int k) {\n",
        "        Map<Integer,Integer> cnt = new HashMap<>();\n",
        "        for(int num:nums){\n",
        "            cnt.put(num,cnt.getOrDefault(num,0)+1);\n",
        "        }\n",
        "        // 初始化PriorityQueue\n",
        "        Queue<Map.Entry<Integer,Integer>> pq = new PriorityQueue<>(\n",
        "            new Comparator<Map.Entry<Integer,Integer>>(){\n",
        "                @Override\n",
        "                public int compare(Map.Entry<Integer,Integer> o1,\n",
        "                Map.Entry<Integer,Integer> o2){\n",
        "                    return o1.getValue() -o2.getValue();\n",
        "                }\n",
        "            }\n",
        "\n",
        "        );\n",
        "        // 将Map.Entry 加入 priorityQueue\n",
        "        Iterator<Map.Entry<Integer,Integer>> iter = cnt.entrySet().iterator();\n",
        "        while(iter.hasNext()){\n",
        "            pq.offer(iter.next());\n",
        "            if(pq.size() > k ){\n",
        "                pq.poll();\n",
        "            }\n",
        "        }\n",
        "        // 赋值\n",
        "        int i =0;\n",
        "        int[] result = new int[k];\n",
        "        for(Map.Entry<Integer,Integer> entry:pq){\n",
        "            result[i] =entry.getKey();\n",
        "            i++;\n",
        "        }\n",
        "        return result;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "50f3bb2f",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1104 lang=java\n",
        " *\n",
        " * [1104] 二叉树寻路\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<Integer> pathInZigZagTree(int label) {\n",
        "        int row = 1, rowStart = 1;\n",
        "        while (rowStart * 2 <= label) {\n",
        "            row++;\n",
        "            rowStart *= 2;\n",
        "        }\n",
        "        if (row % 2 == 0) {\n",
        "            label = getReverse(label, row);\n",
        "        }\n",
        "        List<Integer> path = new ArrayList<Integer>();\n",
        "        while (row > 0) {\n",
        "            if (row % 2 == 0) {\n",
        "                path.add(getReverse(label, row));\n",
        "            } else {\n",
        "                path.add(label);\n",
        "            }\n",
        "            row--;\n",
        "            label >>= 1;\n",
        "        }\n",
        "        Collections.reverse(path);\n",
        "        return path;\n",
        "    }\n",
        "\n",
        "    public int getReverse(int label, int row) {\n",
        "        return (1 << row - 1) + (1 << row) - 1 - label;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9f1180ac",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=105 lang=java\n",
        " *\n",
        " * [105] 从前序与中序遍历序列构造二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    \n",
        "    HashMap<Integer, Integer> valToIndex = new HashMap<>();\n",
        "\n",
        "    public TreeNode buildTree(int[] preorder, int[] inorder) {\n",
        "        for (int i = 0; i < inorder.length; i++) {\n",
        "        valToIndex.put(inorder[i], i);\n",
        "      }\n",
        "        return build(preorder, 0, preorder.length - 1,\n",
        "                 inorder, 0, inorder.length - 1);\n",
        "    }\n",
        "    TreeNode build(int[] preorder, int preStart, int preEnd, \n",
        "               int[] inorder, int inStart, int inEnd) {\n",
        "    if (preStart > preEnd) {\n",
        "        return null;\n",
        "    }\n",
        "\n",
        "    // root 节点对应的值就是前序遍历数组的第一个元素\n",
        "    int rootVal = preorder[preStart];\n",
        "    \n",
        "    // rootVal 在中序遍历数组中的索引\n",
        "    int index = valToIndex.get(rootVal);\n",
        "\n",
        "    int leftSize = index - inStart;\n",
        "\n",
        "    // 先构造出当前根节点\n",
        "    TreeNode root = new TreeNode(rootVal);\n",
        "    // 递归构造左右子树\n",
        "    root.left = build(preorder, preStart + 1, preStart + leftSize,\n",
        "                      inorder, inStart, index - 1);\n",
        "\n",
        "    root.right = build(preorder, preStart + leftSize + 1, preEnd,\n",
        "                       inorder, index + 1, inEnd);\n",
        "    return root;\n",
        "}\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b832dc62",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=210 lang=java\n",
        " *\n",
        " * [210] 课程表 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "     boolean[] visited;\n",
        "    boolean[] onPath;\n",
        "    boolean hasCycle =false;\n",
        "    //记录后序遍历结果\n",
        "    List<Integer> postorder = new ArrayList<>();\n",
        "\n",
        "    public int[] findOrder(int numCourses, int[][] prerequisites) {\n",
        "       //拓扑排序:在初级-->进阶的前提下：就是将后序遍历的结果进行反转\n",
        "        \n",
        "        List<Integer>[] graph = buildGraph(numCourses,prerequisites);\n",
        "        visited = new boolean[numCourses];\n",
        "        onPath = new boolean[numCourses];\n",
        "        //遍历图\n",
        "        for(int i=0;i<numCourses;i++){\n",
        "            traverse(graph,i);\n",
        "        }\n",
        "        //有环图无法进行拓扑排序\n",
        "        if(hasCycle){\n",
        "            return new int[]{};\n",
        "        }\n",
        "        //逆后序遍历结果即为拓扑排序结果\n",
        "        Collections.reverse(postorder);\n",
        "        int[] res = new int[numCourses];\n",
        "        for(int i=0;i<numCourses;i++){\n",
        "            res[i] = postorder.get(i);\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "   \n",
        "\n",
        "    public boolean canFinish(int numCourses, int[][] prerequisites) {\n",
        "        List<Integer>[] graph = buildGraph(numCourses,prerequisites);\n",
        "        visited = new boolean[numCourses];\n",
        "        onPath = new boolean[numCourses];\n",
        "        for(int i=0;i<numCourses;i++){\n",
        "            traverse(graph,i);\n",
        "        }\n",
        "        return !hasCycle;\n",
        "    }\n",
        "    void traverse(List<Integer>[] graph,int s){\n",
        "        if(onPath[s]){\n",
        "            //发现环\n",
        "            hasCycle = true;\n",
        "            return;\n",
        "        }\n",
        "        if(visited[s]) return;\n",
        "\n",
        "        visited[s] =true;\n",
        "        onPath[s] = true;\n",
        "        for(int t:graph[s]){\n",
        "            traverse(graph,t);\n",
        "        }\n",
        "        //后序位置\n",
        "        postorder.add(s);\n",
        "        onPath[s] =false;\n",
        "    }\n",
        "    List<Integer>[] buildGraph(int numCourses,int[][] prerequisites){\n",
        "        List<Integer>[] graph = new LinkedList[numCourses];\n",
        "        for(int i =0;i<numCourses;i++){\n",
        "            graph[i] = new LinkedList<>();\n",
        "        }\n",
        "        for(int[] edge:prerequisites){\n",
        "            int from = edge[1];\n",
        "            int to = edge[0];\n",
        "            graph[from].add(to);\n",
        "        }\n",
        "        return graph;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d596e1b9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2542 lang=java\n",
        " *\n",
        " * [2542] 最大子序列的分数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public long maxScore(int[] nums1, int[] nums2, int k) {\n",
        "        long res =0L;\n",
        "        int n = nums1.length;\n",
        "        //数组 nums2 按降序排列后的下标\n",
        "        Integer[] ids = new Integer[n];\n",
        "        for(int i =0;i<n;i++){\n",
        "            ids[i] = i;\n",
        "        }\n",
        "        Arrays.sort(ids,(i,j)->nums2[j]-nums2[i]);\n",
        "\n",
        "        //num1的前k -1个值的和\n",
        "        //小根堆\n",
        "        PriorityQueue<Integer> minHeap = new PriorityQueue<>();\n",
        "        long sum1 =0L;\n",
        "        for(int i =0;i<k-1;i++){\n",
        "            int id = ids[i];\n",
        "            sum1 += nums1[id];\n",
        "            minHeap.offer(nums1[id]);\n",
        "        }\n",
        "        //从第k个元素(下标 k-1) 开始计算\n",
        "        for(int i =k-1;i<n;i++){\n",
        "            int id = ids[i];\n",
        "            int num1= nums1[id];\n",
        "            //1.加上num1,构成k个数\n",
        "            sum1 += num1;\n",
        "            minHeap.offer(num1);\n",
        "            //2.比较\n",
        "            res =Math.max(sum1 * nums2[id],res);\n",
        "            //3.删除 k 个 数中最小的数\n",
        "            sum1 -= minHeap.poll();\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "90a9d5b3",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1302 lang=java\n",
        " *\n",
        " * [1302] 层数最深叶子节点的和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public int deepestLeavesSum(TreeNode root) {\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "        int sum = 0;\n",
        "        while(!q.isEmpty()){\n",
        "            sum = 0;\n",
        "            int sz = q.size();\n",
        "            \n",
        "            for(int i=0;i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                sum += cur.val;\n",
        "                if(cur.left !=null){\n",
        "                    q.offer(cur.left);\n",
        "                }\n",
        "                if(cur.right !=null){\n",
        "                    q.offer(cur.right);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return sum ;\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "96ca015c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=958 lang=java\n",
        " *\n",
        " * [958] 二叉树的完全性检验\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean isCompleteTree(TreeNode root) {\n",
        "        if (root == null) return false;\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "        TreeNode curr = null;\n",
        "        boolean occur = false;\n",
        "\n",
        "        while(!q.isEmpty()){\n",
        "            curr = q.poll();\n",
        "            if(occur && (curr.left!=null || curr.right!=null)){\n",
        "                return false;\n",
        "            }\n",
        "            //出现左无右有，直接false\n",
        "            if(curr.left ==null && curr.right !=null ){\n",
        "                return false;\n",
        "            }\n",
        "            //左有右无\n",
        "            else if(curr.left != null && curr.right == null){\n",
        "                occur = true;\n",
        "                q.offer(curr.left);\n",
        "            }\n",
        "            //左右都有\n",
        "            else if(curr.left !=null && curr.right !=null){\n",
        "                q.offer(curr.left);\n",
        "                q.offer(curr.right);\n",
        "            }\n",
        "            //左右都无\n",
        "            else if(curr.left == null && curr.right == null){\n",
        "                occur = true;\n",
        "            }\n",
        "         \n",
        "\n",
        "        }\n",
        "        return true;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "47aa8926",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=994 lang=java\n",
        " *\n",
        " * [994] 腐烂的橘子\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int orangesRotting(int[][] grid) {\n",
        "      //1.定义2个int数组，2个一组来记录腐烂橘子的上下左右位置，腐烂橘子(0,0)\n",
        "      //在矩阵中 上{-1,0} 下{1,0} 左{0,-1} 右{0,1}\n",
        "      int[] dx = {-1,1,0,0};\n",
        "      int[] dy = {0,0,-1,1};\n",
        "      int step =0; //感染次数\n",
        "      int flash = 0; //新鲜橘子(后面用于判断是否为-1)\n",
        "\n",
        "       int row = grid.length; //所给矩阵行\n",
        "       int col = grid[0].length; //列\n",
        "\n",
        "       Queue<int[]> queue = new ArrayDeque<>();\n",
        "       //2.遍历矩阵，将所有的腐烂橘子入队，并且记录初始新鲜橘子数\n",
        "       for(int i=0;i<row;i++){\n",
        "           for(int j=0;j<col;j++){\n",
        "              if(grid[i][j] == 1){\n",
        "                flash++;\n",
        "              }\n",
        "           if(grid[i][j] == 2){\n",
        "               queue.offer(new int[]{i,j});\n",
        "           }\n",
        "       }\n",
        "    }\n",
        "    \n",
        "    //3.遍历所有腐烂橘子，同时感染四周\n",
        "    while(flash > 0 && !queue.isEmpty()){//有橘子且队列不空\n",
        "        step++;\n",
        "        //队列中现有的所有腐烂橘子都要进行一次感染\n",
        "        int size = queue.size();\n",
        "        for(int s=0;s<size;s++){\n",
        "            int[] poll = queue.poll();//腐烂橘子\n",
        "            for(int i = 0;i < 4;i++){\n",
        "                // 4个位置dx[i],dy[i], xy 为要感染的橘子位置\n",
        "                int x = poll[0] + dx[i];\n",
        "                int y = poll[1] + dy[i];\n",
        "                if((x >= 0 && x < row) &&(y >= 0 && y < col) && grid[x][y] == 1){\n",
        "                    grid[x][y] =2;\n",
        "                    //把被感染的橘子 入队\n",
        "                    queue.offer(new int[]{x,y});\n",
        "                    flash --;\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "\n",
        "        if(flash > 0 ){\n",
        "            return -1;\n",
        "        }else{\n",
        "            return step;\n",
        "        }\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "398a17c0",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=8 lang=java\n",
        " *\n",
        " * [8] 字符串转换整数 (atoi)\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int myAtoi(String s) {\n",
        "       Automaton automation = new Automaton();\n",
        "       int length = s.length();\n",
        "       for(int i=0;i<length;++i){\n",
        "        automation.get(s.charAt(i));\n",
        "       } \n",
        "       return (int)(automation.sign * automation.ans);\n",
        "    }\n",
        "}\n",
        "class Automaton{\n",
        "    public int sign = 1;\n",
        "    public long ans = 0;\n",
        "    private String state = \"start\";\n",
        "    private Map<String,String[]> table = new HashMap<String,String[]>(){{\n",
        "        put(\"start\",new String[]{\"start\",\"signed\",\"in_number\",\"end\"});\n",
        "        put(\"signed\",new String[]{\"end\",\"end\",\"in_number\",\"end\"});\n",
        "        put(\"in_number\",new String[]{\"end\",\"end\",\"in_number\",\"end\"});\n",
        "        put(\"end\",new String[]{\"end\",\"end\",\"end\",\"end\"});\n",
        "        \n",
        "    }};\n",
        "    public void get(char c){\n",
        "        state = table.get(state)[get_col(c)];\n",
        "        if(\"in_number\".equals(state)){\n",
        "            // 数字字符 转为为数字的方式： '9'-'0','8'-'0'\n",
        "            ans = ans * 10 + c - '0';\n",
        "            // 需要理解 ans 表示绝对值 原值如果为正，ans 与Inter,MAX_VALUE 取最小，如果为负，ans 与-Integer.MIN_VALUE 取最小\n",
        "            ans = sign == 1 ? Math.min(ans,(long)Integer.MAX_VALUE):Math.min(ans,-(long)Integer.MIN_VALUE);\n",
        "        }else if(\"signed\".equals(state)){\n",
        "            sign =c == '+' ? 1: -1;\n",
        "        }\n",
        "    }\n",
        "\n",
        "    private int get_col(char c){\n",
        "        if(c == ' '){\n",
        "            return 0;\n",
        "        }\n",
        "        if(c == '+' || c =='-'){\n",
        "            return 1;\n",
        "        }\n",
        "        if(Character.isDigit(c)){\n",
        "            return 2;\n",
        "        }\n",
        "        return 3;\n",
        "    }\n",
        "}\n",
        "\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8f2e8ca1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1161 lang=java\n",
        " *\n",
        " * [1161] 最大层内元素和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int maxsum =Integer.MIN_VALUE;\n",
        "    int ans = 0;\n",
        "    public int maxLevelSum(TreeNode root) {\n",
        "        bfs(root);\n",
        "        return ans;\n",
        "    }\n",
        "    void bfs(TreeNode root){\n",
        "        if(root == null) return;\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "        int level =0;\n",
        "\n",
        "        while(!q.isEmpty()){\n",
        "            int sz = q.size();\n",
        "            int sum = 0;\n",
        "            level ++;\n",
        "            for(int i=0;i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                sum += cur.val;\n",
        "                if(cur.left !=null){\n",
        "                    q.offer(cur.left);\n",
        "                    \n",
        "                }\n",
        "                if(cur.right !=null){\n",
        "                    q.offer(cur.right);\n",
        "                }\n",
        "            }\n",
        "            if(sum > maxsum){\n",
        "                maxsum = sum;\n",
        "                ans = level;\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5c13a312",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=655 lang=java\n",
        " *\n",
        " * [655] 输出二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        " class Solution {\n",
        "public List<List<String>> printTree(TreeNode root) {\n",
        "    //获取高度\n",
        "    int height = getHeight(root);\n",
        "    String[][] ans = new String[height][(1 << height) - 1];\n",
        "    for (String[] arr : ans) {\n",
        "        Arrays.fill(arr, \"\");\n",
        "    }\n",
        "    List<List<String>> res = new ArrayList<>();\n",
        "    fill(ans, root, 0, 0, ans[0].length);\n",
        "    for (String[] arr : ans) {\n",
        "        res.add(Arrays.asList(arr));\n",
        "    }\n",
        "    return res;\n",
        "}\n",
        "\n",
        "/**\n",
        "    * 填充数组\n",
        "    *\n",
        "    * @param ans  数组\n",
        "    * @param root 根节点\n",
        "    * @param i    第几行\n",
        "    * @param l    左节点\n",
        "    * @param r    右节点\n",
        "    */\n",
        "private void fill(String[][] ans, TreeNode root, int i, int l, int r) {\n",
        "    if (root == null) {\n",
        "        return;\n",
        "    }\n",
        "    ans[i][(l + r) / 2] = \"\" + root.val;\n",
        "    fill(ans, root.left, i + 1, l, (l + r) / 2);\n",
        "    fill(ans, root.right, i + 1, (l + r + 1) / 2, r);\n",
        "}\n",
        "\n",
        "/**\n",
        "    * 获取树的高度\n",
        "    *\n",
        "    * @param root\n",
        "    * @return\n",
        "    */\n",
        "private int getHeight(TreeNode root) {\n",
        "    if (root == null) {\n",
        "        return 0;\n",
        "    }\n",
        "    return Math.max(getHeight(root.left), getHeight(root.right)) + 1;\n",
        "}\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b02b6163",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1071 lang=java\n",
        " *\n",
        " * [1071] 字符串的最大公因子\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String gcdOfStrings(String str1, String str2) {\n",
        "        // 如果两个字符串的拼接结果不同，那么不存在一个字符串可以被两个字符串整除\n",
        "        if (!(str1 + str2).equals(str2 + str1)) {\n",
        "            return \"\";\n",
        "        }\n",
        "\n",
        "        // 如果两个字符串的拼接结果相同，那么最大公约数字符串就是两个字符串的公共前缀\n",
        "        // 这个公共前缀的长度就是两个字符串长度的最大公约数。\n",
        "        return str1.substring(0, gcd(str1.length(), str2.length()));\n",
        "\n",
        "    }\n",
        "    //使用欧几里得算法计算最大公约数\n",
        "    private int gcd(int a, int b) {\n",
        "        return b == 0 ? a : gcd(b, a % b);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "211289ba",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=191 lang=java\n",
        " *\n",
        " * [191] 位1的个数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "public class Solution {\n",
        "    public int hammingWeight(int n) {\n",
        "        int ret = 0;\n",
        "        for (int i = 0; i < 32; i++) {\n",
        "            if ((n & (1 << i)) != 0) {\n",
        "                ret++;\n",
        "            }\n",
        "        }\n",
        "        return ret;\n",
        "    }\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "138eab26",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=643 lang=java\n",
        " *\n",
        " * [643] 子数组最大平均数 I\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public double findMaxAverage(int[] nums, int k) {\n",
        "        //滑动窗口\n",
        "        int sum=0;\n",
        "        int n = nums.length;\n",
        "        for(int i=0;i< k;i++){\n",
        "            sum +=nums[i];\n",
        "        }\n",
        "        int maxSum = sum;\n",
        "        for(int i=k;i<n;i++){\n",
        "            sum = sum -nums[i-k]+nums[i];\n",
        "            maxSum = Math.max(maxSum,sum);\n",
        "        }\n",
        "        return  1.0*maxSum /k;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "567154da",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=103 lang=java\n",
        " *\n",
        " * [103] 二叉树的锯齿形层序遍历\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n",
        "        List<List<Integer>> res = new ArrayList<>();\n",
        "        traverse(root,res);\n",
        "        return res;\n",
        "\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root,List<List<Integer>> res){\n",
        "        if (root == null) return;\n",
        "        // 初始化队列，将 root 加入队列\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        boolean flag = true;\n",
        "        q.offer(root);\n",
        "        // true: 从左往右\n",
        "        // false：从右往左\n",
        "      \n",
        "        while (!q.isEmpty()) {\n",
        "        LinkedList<Integer> levelList = new LinkedList<>();\n",
        "        int sz = q.size();\n",
        "        for(int i=0;i<sz;i++){\n",
        "            TreeNode cur = q.poll();\n",
        "            if(flag){\n",
        "                levelList.addLast(cur.val);\n",
        "            }else{\n",
        "                levelList.addFirst(cur.val);\n",
        "            }\n",
        "\n",
        "            if (cur.left != null) {\n",
        "                 q.offer(cur.left);\n",
        "            }\n",
        "            if (cur.right != null) {\n",
        "                q.offer(cur.right);\n",
        "            }\n",
        "        }\n",
        "        flag = !flag;\n",
        "        res.add(levelList);\n",
        "    }\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1b0b9323",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=114 lang=java\n",
        " *\n",
        " * [114] 二叉树展开为链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public void flatten(TreeNode root) {\n",
        "        //base case\n",
        "        if(root == null) return ;\n",
        "\n",
        "        //利用函数定义 把左右子树拉平\n",
        "        flatten(root.left);\n",
        "        flatten(root.right);\n",
        "\n",
        "        //后序位置\n",
        "        //1、左右子树已经拉成一条链表\n",
        "        TreeNode left = root.left;\n",
        "        TreeNode right = root.right;\n",
        "\n",
        "        //2、将左子树作为右子树\n",
        "        root.left = null;\n",
        "        root.right = left;\n",
        "\n",
        "        //3、将原先的右子树接到当前右子树的末端\n",
        "        TreeNode p  =root;\n",
        "        while(p.right!=null){\n",
        "            p = p.right;\n",
        "        }\n",
        "        p.right =right;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8d848ec5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2300 lang=java\n",
        " *\n",
        " * [2300] 咒语和药水的成功对数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] successfulPairs(int[] spells, int[] potions, long success) {\n",
        "        Arrays.sort(potions);\n",
        "        int n = spells.length, m = potions.length;\n",
        "        int[] res = new int[n];\n",
        "        for (int i = 0; i < n; i++) {\n",
        "            long t = (success + spells[i] - 1) / spells[i] - 1;\n",
        "            // long t = success /spells[i]+1;\n",
        "            res[i] = m - binarySearch(potions, 0, m - 1, t);\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    public int binarySearch(int[] arr, int lo, int hi, long target) {\n",
        "        int res = hi + 1;\n",
        "        while (lo <= hi) {\n",
        "            int mid = lo + (hi - lo) / 2;\n",
        "            if (arr[mid] > target) {\n",
        "                res = mid;\n",
        "                hi = mid - 1;\n",
        "            } else {\n",
        "                lo = mid + 1;\n",
        "            }\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f52745dc",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1080 lang=java\n",
        " *\n",
        " * [1080] 根到叶路径上的不足节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    //全局变量，方便操作\n",
        "    int limit;\n",
        "\n",
        "    public TreeNode sufficientSubset(TreeNode root, int num) {\n",
        "        limit = num;\n",
        "        //递归遍历左节点，右节点\n",
        "        root.left = dfs(root.left, root.val);\n",
        "        root.right = dfs(root.right, root.val);\n",
        "        //最后再进行一次判断，判断根节点是否需要删除\n",
        "        if (root.left == null && root.right == null) {\n",
        "            return null;\n",
        "        }\n",
        "        return root;\n",
        "    }\n",
        "\n",
        "    public TreeNode dfs(TreeNode root, int sum) {\n",
        "        //空节点，直接返回null\n",
        "        if (root == null) {\n",
        "            return null;\n",
        "        }\n",
        "        //叶子节点，判断到达此叶子节点时，路径总和是否小于limit\n",
        "        //小于返回null，大于返回root\n",
        "        if (root.left == null && root.right == null) {\n",
        "            return sum + root.val < limit ? null : root;\n",
        "        }\n",
        "        //向下进行深度优先递归\n",
        "        root.left = dfs(root.left, sum + root.val);\n",
        "        root.right = dfs(root.right, sum + root.val);\n",
        "        //最后一次判断，判断当前节点是否需要删除\n",
        "        //左右节点都为null，证明经过该节点的两条路径得到的总和小于0\n",
        "        //因此向上返回一个null\n",
        "        if (root.left == null && root.right == null) {\n",
        "            return null;\n",
        "        }\n",
        "        //否则返回该节点\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c101ae77",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=64 lang=java\n",
        " *\n",
        " * [64] 最小路径和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int minPathSum(int[][] grid) {\n",
        "        for(int i=0;i<grid.length;i++){\n",
        "            for(int j =0;j<grid[0].length;j++){\n",
        "                if(i == 0 && j == 0) continue;\n",
        "                else if(i ==0) grid[i][j]  =grid[i][j-1] + grid[i][j];\n",
        "                else if(j == 0 ) grid[i][j]  =grid[i-1][j] + grid[i][j];\n",
        "                else grid[i][j] = Math.min(grid[i-1][j],grid[i][j-1])+grid[i][j];\n",
        "            }\n",
        "        }\n",
        "        return grid[grid.length -1][grid[0].length -1];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b7a7cc92",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=118 lang=java\n",
        " *\n",
        " * [118] 杨辉三角\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<Integer>> generate(int numRows) {\n",
        "        List<List<Integer>> res = new ArrayList<>();\n",
        "\n",
        "        for(int i=0;i<numRows;i++){\n",
        "            List<Integer> ans = new ArrayList<>();\n",
        "            for(int j =0; j<=i;j++){\n",
        "                if(j == 0 || j ==i){\n",
        "                    ans.add(1);\n",
        "                }else{\n",
        "                    ans.add(res.get(i-1).get(j-1)+res.get(i-1).get(j));\n",
        "                }\n",
        "            }\n",
        "            res.add(ans);\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a4f7e59c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=16 lang=java\n",
        " *\n",
        " * [16] 最接近的三数之和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int threeSumClosest(int[] nums, int target) {\n",
        "        if(nums.length < 3) return 0;\n",
        "\n",
        "        //排序\n",
        "        Arrays.sort(nums);\n",
        "        //记录三数之和与目标的偏差\n",
        "        int delta = Integer.MAX_VALUE;\n",
        "        for(int i =0; i<nums.length-2;i++){\n",
        "            //固定nums[i] 为三数之和中的第一个数\n",
        "            //r然后对nums[i+1..] 搜索接近target - nums[i] 的两数之和\n",
        "            int sum = nums[i] + twoSumClosest(nums, i+1, target-nums[i]);\n",
        "            if(Math.abs(delta) > Math.abs(target-sum)){\n",
        "                delta = target - sum;\n",
        "            }\n",
        "        }\n",
        "        return target - delta;\n",
        "        \n",
        "    }\n",
        "\n",
        "    //在nums[start..] 搜索最接近target 的两数之和\n",
        "    int twoSumClosest(int[] nums,int start,int target){\n",
        "        int lo =start,hi=nums.length-1;;\n",
        "        int delta = Integer.MAX_VALUE;\n",
        "        while(lo <hi){\n",
        "            int sum = nums[lo] + nums[hi];\n",
        "            if(Math.abs(delta) > Math.abs(target-sum)){\n",
        "                delta = target - sum;\n",
        "            }\n",
        "            if(sum < target){\n",
        "                lo++;\n",
        "            }else{\n",
        "                hi--;\n",
        "            }\n",
        "        }\n",
        "        return target - delta;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9f8a859f",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=979 lang=java\n",
        " *\n",
        " * [979] 在二叉树中分配硬币\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "\tint ans;\n",
        "\n",
        "\tpublic int distributeCoins(TreeNode root) {\n",
        "\t\tans = 0;\n",
        "\t\tthis.find(root);\n",
        "\t\treturn ans;\n",
        "\t}\n",
        "\t\n",
        "\tprivate int find(TreeNode root) {\n",
        "\t\tif (root == null) {\n",
        "\t\t\treturn 0;\n",
        "\t\t}\n",
        "\t\tint cur = root.val;\n",
        "\t\tint left = this.find(root.left);\n",
        "\t\tint right = this.find(root.right);\n",
        "\t\tint sum = cur + left + right - 1;\n",
        "\t\tans += Math.abs(sum);\n",
        "\t\treturn sum;\n",
        "\t}\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b54f46d1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=74 lang=java\n",
        " *\n",
        " * [74] 搜索二维矩阵\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean searchMatrix(int[][] matrix, int target) {\n",
        "        //坐标轴法\n",
        "        int rows= matrix.length -1, columns = 0;\n",
        "        while(rows >=0 && columns< matrix[0].length){\n",
        "            int num = matrix[rows][columns];\n",
        "            if(num == target){\n",
        "                return true;\n",
        "            }else if(num > target){\n",
        "                rows --;\n",
        "            }else{\n",
        "                columns ++;\n",
        "            }\n",
        "        }\n",
        "        return false;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1f481eeb",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1110 lang=java\n",
        " *\n",
        " * [1110] 删点成林\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "      public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {\n",
        "        List<TreeNode> list = new LinkedList<>();\n",
        "        Set<Integer> set = new HashSet<>();\n",
        "        // 将delete转换为set方便进行判断\n",
        "        for(int num : to_delete){\n",
        "            set.add(num);\n",
        "        }\n",
        "        // 如果什么都不需要删掉，那么结果集中应该包含原本这棵树\n",
        "        list.add(root);\n",
        "        del(root, set, list);\n",
        "        return list;\n",
        "    }\n",
        "\n",
        "    TreeNode del(TreeNode node, Set<Integer> delete, List<TreeNode> list){\n",
        "        //base case\n",
        "        if(node == null) return null;\n",
        "        \n",
        "        node.left = del(node.left, delete, list);\n",
        "        node.right = del(node.right, delete,list);\n",
        "        \n",
        "        //后序位置\n",
        "        if(delete.contains(node.val)){\n",
        "            if(node.left != null){\n",
        "                list.add(node.left);\n",
        "            }\n",
        "            if(node.right != null){\n",
        "                list.add(node.right);\n",
        "            }\n",
        "            if(list.contains(node)){\n",
        "                list.remove(node);\n",
        "            }\n",
        "            return null;\n",
        "        }\n",
        "        return node;\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "52b620e7",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=652 lang=java\n",
        " *\n",
        " * [652] 寻找重复的子树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    //记录所有子树以及出现的次数\n",
        "    HashMap<String,Integer> memo = new HashMap<>();\n",
        "    //记录重复的子树根节点\n",
        "    List<TreeNode> res = new LinkedList<>();\n",
        "\n",
        "    public List<TreeNode> findDuplicateSubtrees(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return res;\n",
        "    }\n",
        "    String traverse(TreeNode root){\n",
        "        if(root == null) return \"#\";\n",
        "\n",
        "        String left = traverse(root.left);\n",
        "        String right = traverse(root.right);\n",
        "        /* 后序位 */\n",
        "        String subTree = left+\",\"+right+\",\"+root.val;\n",
        "        int freq = memo.getOrDefault(subTree,0);\n",
        "        if(freq == 1){\n",
        "            res.add(root);\n",
        "        }\n",
        "        //给子树对应的出现次数加一\n",
        "        memo.put(subTree,freq+1);\n",
        "        return subTree;\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "680baf88",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=437 lang=java\n",
        " *\n",
        " * [437] 路径总和 III\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public int pathSum(TreeNode root, int targetSum) {\n",
        "        Map<Long, Integer> prefix = new HashMap<Long, Integer>();\n",
        "        prefix.put(0L, 1);\n",
        "        return dfs(root, prefix, 0, targetSum);\n",
        "    }\n",
        "\n",
        "    public int dfs(TreeNode root, Map<Long, Integer> prefix, long curr, int targetSum) {\n",
        "        if (root == null) {\n",
        "            return 0;\n",
        "        }\n",
        "\n",
        "        int ret = 0;\n",
        "        curr += root.val;\n",
        "\n",
        "        ret = prefix.getOrDefault(curr - targetSum, 0);\n",
        "        prefix.put(curr, prefix.getOrDefault(curr, 0) + 1);\n",
        "\n",
        "        ret += dfs(root.left, prefix, curr, targetSum);\n",
        "        ret += dfs(root.right, prefix, curr, targetSum);\n",
        "        \n",
        "        prefix.put(curr, prefix.getOrDefault(curr, 0) - 1);\n",
        "\n",
        "        return ret;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b379adf2",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2 lang=java\n",
        " *\n",
        " * [2] 两数相加\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\n",
        "        ListNode head1 = l1;\n",
        "        ListNode head2 = l2;\n",
        "\n",
        "        ListNode dummy = new ListNode(-1),p =dummy;\n",
        "        int jin =0;\n",
        "\n",
        "        while(head1!=null || head2!=null){\n",
        "             int num1 = 0,num2 =0;\n",
        "            if(head1 != null){\n",
        "               num1 =  head1.val;\n",
        "               head1 = head1.next;\n",
        "            }\n",
        "\n",
        "            if(head2 != null){\n",
        "               num2 =  head2.val;\n",
        "               head2 = head2.next;\n",
        "            }\n",
        "            int res =  (num1+num2+jin)%10;\n",
        "            p.next = new ListNode(res);\n",
        "            p = p.next;\n",
        "            jin = (num1+num2+jin)/10;\n",
        "            \n",
        "        }\n",
        "        if(jin >0 ){\n",
        "            p.next = new ListNode(jin);\n",
        "        }\n",
        "        return dummy.next;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e5365a94",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=11 lang=java\n",
        " *\n",
        " * [11] 盛最多水的容器\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int maxArea(int[] height) {\n",
        "        int i= 0,j=height.length-1,res=0;\n",
        "       //双指针移动\n",
        "       // 谁小，谁向内移动\n",
        "       while(i < j){\n",
        "           res = height[i] < height[j]?\n",
        "           Math.max(res,(j-i) * height[i++]):\n",
        "           Math.max(res,(j-i) * height[j--]);\n",
        "       }\n",
        "       return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c4c71bca",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=34 lang=java\n",
        " *\n",
        " * [34] 在排序数组中查找元素的第一个和最后一个位置\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] searchRange(int[] nums, int target) {\n",
        "        int[] result = {-1,-1};\n",
        "        for(int i =0;i<nums.length;i++){\n",
        "            int item = nums[i];\n",
        "            if(result[0] == -1 &&item == target){\n",
        "                result[0] = i;\n",
        "                result[1] = i;\n",
        "            }\n",
        "            if(result[0] != -1 &&item == target){\n",
        "                result[1] = i;\n",
        "            }\n",
        "        }\n",
        "        return result;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "47887db4",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=238 lang=java\n",
        " *\n",
        " * [238] 除自身以外数组的乘积\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] productExceptSelf(int[] nums) {\n",
        "        //左右乘积\n",
        "\n",
        "        int length = nums.length;\n",
        "        //L和R分别表示左右两侧的乘积列表\n",
        "        int[] L = new int[length];\n",
        "        int[] R = new int[length];\n",
        "\n",
        "        int[] answer = new int[length];\n",
        "\n",
        "        //L[i]为索引 i 左侧所有元素的乘积\n",
        "        //对于索引为 0 的元素，因为左侧没有元素，所有L[0] =1\n",
        "        L[0] =1;\n",
        "        for(int i =1;i<length;i++){\n",
        "            L[i] = nums[i-1] * L[i-1];\n",
        "        }\n",
        "\n",
        "        //R[i]为索引i 右侧所有元素的乘积\n",
        "        R[length -1] = 1;\n",
        "        for(int i = length -2;i>=0;i--){\n",
        "            R[i] = nums[i+1] * R[i+1];\n",
        "        }\n",
        "        // 对于索引 i，除 nums[i]之外其余各元素的乘积就是左边\n",
        "        for(int i =0; i< length;i++){\n",
        "            answer[i] =L[i] * R[i];\n",
        "        }\n",
        "        return answer;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a2e12cbd",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=671 lang=java\n",
        " *\n",
        " * [671] 二叉树中第二小的节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int ans =-1;\n",
        "    int rootvalue;\n",
        "    boolean flag =false;\n",
        "\n",
        "    public int findSecondMinimumValue(TreeNode root) {\n",
        "        rootvalue = root.val;\n",
        "        dfs(root);\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public void dfs(TreeNode node) {\n",
        "        if (node == null) {\n",
        "            return;\n",
        "        }\n",
        "        //先找到比root大的有效值\n",
        "        if(node.val > rootvalue &&!flag){\n",
        "            flag = true;\n",
        "            ans = node.val;\n",
        "        }\n",
        "        //然后再进行与ans 比较\n",
        "         if(node.val > rootvalue &&flag){\n",
        "            ans = Math.min(node.val,ans);\n",
        "         }\n",
        "\n",
        "        dfs(node.left);\n",
        "        dfs(node.right);\n",
        "    }\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d6d25953",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=215 lang=java\n",
        " *\n",
        " * [215] 数组中的第K个最大元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int findKthLargest(int[] nums, int k) {\n",
        "        //首先随机打乱数组\n",
        "        shuffle(nums);\n",
        "        int lo = 0, hi = nums.length-1;\n",
        "        //转化成[排名第K的元素]\n",
        "        k = nums.length-k;\n",
        "        while(lo <= hi){\n",
        "            //在nums[lo..hi]中选一个分界点\n",
        "            int p = partition(nums,lo,hi);\n",
        "            if(p <k){\n",
        "                //第k 大的元素在nums[p+1..hi]中\n",
        "                lo = p+1;\n",
        "            }else if(p > k){\n",
        "                //第k大的元素在nums[lo..p-1]中\n",
        "                hi = p-1;\n",
        "            }else{\n",
        "                //找到第k大的元素\n",
        "                return nums[p];\n",
        "            }\n",
        "        }\n",
        "        return -1;\n",
        "    }\n",
        "    private static int partition(int[] nums,int lo,int hi){\n",
        "        int pivot = nums[lo];\n",
        "        //关于区间边界控制需格外小心，稍有不慎就会出错\n",
        "        //我这里把i,j 定义为开区间，同时定义:\n",
        "        //[lo,i]<=pivot;(j,hi] > pivot\n",
        "        //之后都要正确维护这个边界区间的定义\n",
        "        int i = lo+1,j=hi;\n",
        "        //当i > j 时结束循环，以保证区间[lo,hi]都被覆盖\n",
        "        while(i <=j){\n",
        "            while(i<hi&& nums[i] <=pivot) i++;\n",
        "            //此时 while 结束时恰好 nums[i] > pivot\n",
        "            while(j>lo&& nums[j] > pivot) j--;\n",
        "            //此时while 结束恰好 nums[j] <= pivot\n",
        "            //此时[lo,i) <= pivot &&(j,hi] > pivot\n",
        "            if(i >= j) break;\n",
        "\n",
        "            swap(nums,i,j);\n",
        "        }\n",
        "        //将pivot 放到合适的位置，即pivot 左边元素较小，右边元素较大\n",
        "        swap(nums,lo,j);\n",
        "        return j;\n",
        "    }\n",
        "\n",
        "\n",
        "    //洗牌算法，将输入的数组随机打乱\n",
        "    private static void shuffle(int[] nums){\n",
        "        Random rand = new Random();\n",
        "        int n = nums.length;\n",
        "        for(int i =0;i<n;i++){\n",
        "            //生成[i,n-i]的随机数\n",
        "            int r = i + rand.nextInt(n-i);\n",
        "            swap(nums,i,r);\n",
        "\n",
        "        }\n",
        "    }\n",
        "\n",
        "    private static void swap(int[] nums,int i,int j){\n",
        "        int temp = nums[j];\n",
        "        nums[j] = nums[i];\n",
        "        nums[i] = temp;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2cf0a704",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=20 lang=java\n",
        " *\n",
        " * [20] 有效的括号\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean isValid(String s) {\n",
        "        int n = s.length();\n",
        "        if(n % 2 ==1){\n",
        "            return false;\n",
        "        }\n",
        "\n",
        "        Map<Character,Character> pairs = new HashMap<Character,Character>(){\n",
        "            {\n",
        "                put(')','(');\n",
        "                put(']','[');\n",
        "                put('}','{');\n",
        "            }\n",
        "        };\n",
        "        Deque<Character> stack = new LinkedList<Character>();\n",
        "        for(int i=0;i<n;i++){\n",
        "            char ch = s.charAt(i);\n",
        "            if(pairs.containsKey(ch)){\n",
        "                if(stack.isEmpty() || stack.peek() != pairs.get(ch)){\n",
        "                    return false;\n",
        "                }\n",
        "                stack.pop();\n",
        "            }else{\n",
        "                stack.push(ch);\n",
        "            }\n",
        "        }\n",
        "        return stack.isEmpty();\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9c850595",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=6 lang=java\n",
        " *\n",
        " * [6] Z 字形变换\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String convert(String s, int numRows) {\n",
        "        int n =s.length(),r = numRows;\n",
        "        if(r == 1 || r >=n ){\n",
        "            return s;\n",
        "        }\n",
        "        int t = r * 2 -2;\n",
        "        int c = (n+t-1)/t *(r-1);\n",
        "        char[][] mat = new char[r][c];\n",
        "        for(int i=0,x=0,y=0;i<n;++i){\n",
        "            mat[x][y] = s.charAt(i);\n",
        "            if(i%t < r -1){\n",
        "                ++x;//向下移动\n",
        "            }else{\n",
        "                --x;\n",
        "                ++y;//向右上移动\n",
        "            }\n",
        "        }\n",
        "        StringBuffer ans = new StringBuffer();\n",
        "        for(char[] row:mat){\n",
        "            for(char ch:row){\n",
        "                if(ch!=0){\n",
        "                    ans.append(ch);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return ans.toString();\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c177d670",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=98 lang=java\n",
        " *\n",
        " * [98] 验证二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean isValidBST(TreeNode root) {\n",
        "        //null 用的好\n",
        "       return isValidBST(root,null,null);\n",
        "    }\n",
        "\n",
        "    boolean isValidBST(TreeNode root,TreeNode min,TreeNode max){\n",
        "        //base case\n",
        "        if(root == null) return true;\n",
        "        if(min!=null && min.val >= root.val) return false;\n",
        "        if(max!=null&& max.val <= root.val) return false;\n",
        "        return isValidBST(root.left,min,root) &&\n",
        "               isValidBST(root.right,root,max);\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1f68e801",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=162 lang=java\n",
        " *\n",
        " * [162] 寻找峰值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int findPeakElement(int[] nums) {\n",
        "       int n = nums.length;\n",
        "       int left = 0,right = n-1,ans = -1;\n",
        "       while(left <=right){\n",
        "           int mid = left +(right -left)/2;\n",
        "           if(compare(nums,mid-1,mid) <0\n",
        "           && compare(nums,mid,mid+1)>0){\n",
        "               ans =mid;\n",
        "               break;\n",
        "           }\n",
        "           if(compare(nums,mid,mid+1)<0){\n",
        "               left = mid +1;\n",
        "           }else{\n",
        "               right = mid -1;\n",
        "           }\n",
        "       }\n",
        "       return ans;\n",
        "    }\n",
        "\n",
        "    public int[] get(int[] nums,int idx){\n",
        "        if(idx == -1 || idx == nums.length){\n",
        "            return new int[]{0,0};\n",
        "        }\n",
        "        return new int[]{1,nums[idx]};\n",
        "    }\n",
        "\n",
        "    public int compare(int[] nums,int idx1,int idx2){\n",
        "        int[] num1 = get(nums,idx1);\n",
        "        int[] num2 = get(nums,idx2);\n",
        "        if(num1[0]!=num2[0]){\n",
        "            return num1[0] > num2[0]?1:-1;\n",
        "        }\n",
        "        if(num1[1] == num2[1]){\n",
        "            return 0;\n",
        "        }\n",
        "        return num1[1] >num2[1]?1:-1;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5e89a110",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=240 lang=java\n",
        " *\n",
        " * [240] 搜索二维矩阵 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean searchMatrix(int[][] matrix, int target) {\n",
        "        int m = matrix.length, n = matrix[0].length;\n",
        "        // 初始化在右上角\n",
        "        int i = 0, j = n - 1;\n",
        "        while (i < m && j >= 0) {\n",
        "            if (matrix[i][j] == target) {\n",
        "                return true;\n",
        "            }\n",
        "            if (matrix[i][j] < target) {\n",
        "                // 需要大一点，往下移动\n",
        "                i++;\n",
        "            } else {\n",
        "                // 需要小一点，往左移动\n",
        "                j--;\n",
        "            }\n",
        "        }\n",
        "        // while 循环中没有找到，则 target 不存在\n",
        "        return false;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9dfd361a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=739 lang=java\n",
        " *\n",
        " * [739] 每日温度\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] dailyTemperatures(int[] temperatures) {\n",
        "        //递减栈\n",
        "        Stack<Integer> stack = new Stack<>();\n",
        "        int length = temperatures.length;\n",
        "        int[] result = new int[length];\n",
        "        for(int i=0;i<length;i++){\n",
        "            while(!stack.isEmpty()&&temperatures[i]> temperatures[stack.peek()]){\n",
        "                int pre = stack.pop();\n",
        "                result [pre] = i - pre;\n",
        "            }\n",
        "            stack.add(i);\n",
        "        }\n",
        "        return result;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1d21b5c5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=687 lang=java\n",
        " *\n",
        " * [687] 最长同值路径\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int res;\n",
        "\n",
        "    public int longestUnivaluePath(TreeNode root) {\n",
        "        res = 0;\n",
        "        dfs(root);\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    public int dfs(TreeNode root) {\n",
        "        if (root == null) {\n",
        "            return 0;\n",
        "        }\n",
        "        int left = dfs(root.left);\n",
        "        int right = dfs(root.right);\n",
        "        int left1 = 0, right1 = 0;\n",
        "        //左子树 \n",
        "        if (root.left != null && root.left.val == root.val) {\n",
        "            left1 = left + 1;\n",
        "        }\n",
        "        //右子树\n",
        "        if (root.right != null && root.right.val == root.val) {\n",
        "            right1 = right + 1;\n",
        "        }\n",
        "\n",
        "        res = Math.max(res, left1 + right1);\n",
        "        return Math.max(left1, right1);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c55583a9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=23 lang=java\n",
        " *\n",
        " * [23] 合并 K 个升序链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode mergeKLists(ListNode[] lists) {\n",
        "        if(lists.length == 0) return null;\n",
        "        //虚拟头节点\n",
        "        ListNode dummy = new ListNode(-1);\n",
        "        ListNode p = dummy;\n",
        "\n",
        "        PriorityQueue<ListNode> pq = new PriorityQueue<>(\n",
        "            lists.length,(a,b)->(a.val -b.val)\n",
        "        );\n",
        "        for(ListNode head:lists){\n",
        "            if(head!=null){\n",
        "                pq.add(head);\n",
        "            }\n",
        "            \n",
        "        }\n",
        "\n",
        "        while(!pq.isEmpty()){\n",
        "            //获取最小节点，接到结果链表中\n",
        "            ListNode node = pq.poll();\n",
        "            p.next= node ;\n",
        "            if(node.next !=null){\n",
        "                pq.add(node.next);\n",
        "            }\n",
        "            p=p.next;\n",
        "    }\n",
        "    return dummy.next;\n",
        "}}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bdfb6aa5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=216 lang=java\n",
        " *\n",
        " * [216] 组合总和 III\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<Integer>> combinationSum3(int k, int n) {\n",
        "       List<List<Integer>> result = new ArrayList<>();\n",
        "       List<Integer> path = new ArrayList<>();\n",
        "\n",
        "       dfs(1,k,n,path,result);\n",
        "       return result;\n",
        "    }\n",
        "\n",
        "    public void dfs(int index,int k,int n,List<Integer> path,List<List<Integer>> result){\n",
        "        if(n == 0 && path.size() == k){\n",
        "            //由于path是动态的，需要在result.add的时候使用new ArrayList<>(path),\n",
        "            //复制到新的对象中。\n",
        "            result.add(new ArrayList<>(path));\n",
        "            return ;\n",
        "        }\n",
        "\n",
        "        for(int i = index;i<10;i++){\n",
        "            path.add(i);\n",
        "            // 每个数在一个结果中只能使用一个 所以i+1\n",
        "            dfs(i+1,k,n-i,path,result);\n",
        "            path.remove(path.size()-1);\n",
        "        }\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8cade29c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1466 lang=java\n",
        " *\n",
        " * [1466] 重新规划路线\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int minReorder(int n, int[][] connections) {\n",
        "     List<int[]>[] e = new List[n];\n",
        "     for(int i=0;i<n;i++){\n",
        "         e[i] = new ArrayList<int[]>();\n",
        "     }\n",
        "     for(int[] edge:connections){\n",
        "         e[edge[0]].add(new int[]{edge[1],1});\n",
        "         e[edge[1]].add(new int[]{edge[0],0});\n",
        "     }\n",
        "     return dfs(0,-1,e);\n",
        "    }\n",
        "\n",
        "    public int dfs(int x,int parent,List<int[]>[] e){\n",
        "        int res = 0;\n",
        "        for(int[] edge:e[x]){\n",
        "            if(edge[0] == parent){\n",
        "                continue;\n",
        "            }\n",
        "            res += edge[1] + dfs(edge[0],x,e);\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "89915b36",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1038 lang=java\n",
        " *\n",
        " * [1038] 从二叉搜索树到更大和树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode bstToGst(TreeNode root) {\n",
        "         traverse(root);\n",
        "         return root;\n",
        "    }\n",
        "    \n",
        "    // 记录累加和\n",
        "    int sum = 0;\n",
        "    void traverse(TreeNode root){\n",
        "        //base case\n",
        "        if(root == null) return;\n",
        "        traverse(root.right);\n",
        "        \n",
        "         /** 中序位置 */\n",
        "        sum+= root.val;\n",
        "        //将BST 转化为累加树\n",
        "        root.val =sum;\n",
        "\n",
        "        traverse(root.left);\n",
        "      \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9b6e60d8",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2352 lang=java\n",
        " *\n",
        " * [2352] 相等行列对\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int equalPairs(int[][] grid) {\n",
        "        int n = grid.length;\n",
        "        Map<List<Integer>, Integer> cnt = new HashMap<List<Integer>, Integer>();\n",
        "        for (int[] row : grid) {\n",
        "            List<Integer> arr = new ArrayList<Integer>();\n",
        "            for (int num : row) {\n",
        "                arr.add(num);\n",
        "            }\n",
        "            cnt.put(arr, cnt.getOrDefault(arr, 0) + 1);\n",
        "        }\n",
        "\n",
        "        int res = 0;\n",
        "        for (int j = 0; j < n; j++) {\n",
        "            List<Integer> arr = new ArrayList<Integer>();\n",
        "            for (int i = 0; i < n; i++) {\n",
        "                arr.add(grid[i][j]);\n",
        "            }\n",
        "            if (cnt.containsKey(arr)) {\n",
        "                res += cnt.get(arr);\n",
        "            }\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8204f4d5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=108 lang=java\n",
        " *\n",
        " * [108] 将有序数组转换为二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        " //二叉搜索树的中序排序是升序的\n",
        "class Solution {\n",
        "     int rightDepth = 0;\n",
        "     int leftDepth  = 0;\n",
        "    public TreeNode sortedArrayToBST(int[] nums) {\n",
        "\n",
        "        return  build(nums,0,nums.length-1);\n",
        "\n",
        "    }\n",
        "\n",
        "    TreeNode build(int[] nums,int lo,int hi){\n",
        "        //base case\n",
        "        if(nums.length==0) return null;\n",
        "\n",
        "        if(lo > hi){ return null;}\n",
        "\n",
        "        int mid = (lo+hi)/2;\n",
        "        TreeNode root = new TreeNode(nums[mid]);\n",
        "        root.left = build(nums,lo,mid-1);\n",
        "        root.right =build(nums,mid+1,hi);\n",
        "     \n",
        "        return root;\n",
        "\n",
        "    }\n",
        "\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "11cc9051",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=207 lang=java\n",
        " *\n",
        " * [207] 课程表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    boolean[] visited;\n",
        "    boolean[] onPath;\n",
        "    boolean hasCycle =false;\n",
        "\n",
        "    public boolean canFinish(int numCourses, int[][] prerequisites) {\n",
        "        List<Integer>[] graph = buildGraph(numCourses,prerequisites);\n",
        "        visited = new boolean[numCourses];\n",
        "        onPath = new boolean[numCourses];\n",
        "        for(int i=0;i<numCourses;i++){\n",
        "            traverse(graph,i);\n",
        "        }\n",
        "        return !hasCycle;\n",
        "    }\n",
        "    void traverse(List<Integer>[] graph,int s){\n",
        "        if(onPath[s]){\n",
        "            //发现环\n",
        "            hasCycle = true;\n",
        "            return;\n",
        "        }\n",
        "        if(visited[s]) return;\n",
        "\n",
        "        visited[s] =true;\n",
        "        onPath[s] = true;\n",
        "        for(int t:graph[s]){\n",
        "            traverse(graph,t);\n",
        "        }\n",
        "        onPath[s] =false;\n",
        "    }\n",
        "    List<Integer>[] buildGraph(int numCourses,int[][] prerequisites){\n",
        "        List<Integer>[] graph = new LinkedList[numCourses];\n",
        "        for(int i =0;i<numCourses;i++){\n",
        "            graph[i] = new LinkedList<>();\n",
        "        }\n",
        "        for(int[] edge:prerequisites){\n",
        "            int from = edge[1];\n",
        "            int to = edge[0];\n",
        "            graph[from].add(to);\n",
        "        }\n",
        "        return graph;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9dec6c88",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=449 lang=java\n",
        " *\n",
        " * [449] 序列化和反序列化二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "public class Codec {\n",
        "    String SEP=\",\";\n",
        "    String NULL = \"null\";\n",
        "    // Encodes a tree to a single string.\n",
        "    public String serialize(TreeNode root) {\n",
        "        StringBuilder sb = new StringBuilder();\n",
        "        serialize(root,sb);\n",
        "        return sb.toString();\n",
        "    }\n",
        "    void serialize(TreeNode root,StringBuilder sb){\n",
        "        if(root == null){\n",
        "            sb.append(NULL).append(SEP);\n",
        "            return;\n",
        "        }\n",
        "        sb.append(root.val).append(SEP);\n",
        "\n",
        "        serialize(root.left,sb);\n",
        "        serialize(root.right,sb);\n",
        "\n",
        "    }\n",
        "\n",
        "\n",
        "\n",
        "    // Decodes your encoded data to tree.\n",
        "    public TreeNode deserialize(String data) {\n",
        "        LinkedList<String> nodes = new LinkedList<>();\n",
        "        for(String s:data.split(SEP)){\n",
        "            nodes.add(s);\n",
        "        }\n",
        "        return deserialize(nodes);\n",
        "        \n",
        "    }\n",
        "    TreeNode deserialize(LinkedList<String> nodes){\n",
        "        if(nodes.isEmpty()) return null;\n",
        "        /*  前序位置 */\n",
        "        //列表最左侧就是根节点\n",
        "        String first = nodes.removeFirst();\n",
        "        if(first.equals(NULL)) return null;\n",
        "        TreeNode root = new TreeNode(Integer.parseInt(first));\n",
        "        /* */\n",
        "        root.left = deserialize(nodes);\n",
        "        root.right=deserialize(nodes);\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "\n",
        "// Your Codec object will be instantiated and called as such:\n",
        "// Codec ser = new Codec();\n",
        "// Codec deser = new Codec();\n",
        "// String tree = ser.serialize(root);\n",
        "// TreeNode ans = deser.deserialize(tree);\n",
        "// return ans;\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2f7117bd",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=208 lang=java\n",
        " *\n",
        " * [208] 实现 Trie (前缀树)\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Trie {\n",
        "    private Trie[] children;\n",
        "    private boolean isEnd;\n",
        "\n",
        "    public Trie() {\n",
        "        children = new Trie[26];\n",
        "        isEnd =false;\n",
        "    }\n",
        "    \n",
        "    public void insert(String word) {\n",
        "        Trie node = this;\n",
        "        for(int i=0;i<word.length();i++){\n",
        "            char ch = word.charAt(i);\n",
        "            int index =ch - 'a';\n",
        "            if(node.children[index] == null){\n",
        "                node.children[index] = new Trie();\n",
        "            }\n",
        "            node = node.children[index];\n",
        "        }\n",
        "        node.isEnd = true;\n",
        "    }\n",
        "    \n",
        "    public boolean search(String word) {\n",
        "        Trie node =searchPrefix(word);\n",
        "        return node != null && node.isEnd;\n",
        "    }\n",
        "    \n",
        "    public boolean startsWith(String prefix) {\n",
        "        return searchPrefix(prefix) != null;\n",
        "    }\n",
        "\n",
        "    private Trie searchPrefix(String prefix){\n",
        "        Trie node = this;\n",
        "        for(int i=0;i<prefix.length();i++){\n",
        "            char ch = prefix.charAt(i);\n",
        "            int index = ch - 'a';\n",
        "            if(node.children[index] == null){\n",
        "                return null;\n",
        "            }\n",
        "            node = node.children[index];\n",
        "        }\n",
        "        return node;\n",
        "    }\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your Trie object will be instantiated and called as such:\n",
        " * Trie obj = new Trie();\n",
        " * obj.insert(word);\n",
        " * boolean param_2 = obj.search(word);\n",
        " * boolean param_3 = obj.startsWith(prefix);\n",
        " */\n",
        "\n",
        "/**\n",
        " * Your Trie object will be instantiated and called as such:\n",
        " * Trie obj = new Trie();\n",
        " * obj.insert(word);\n",
        " * boolean param_2 = obj.search(word);\n",
        " * boolean param_3 = obj.startsWith(prefix);\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a42f6f7a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1008 lang=java\n",
        " *\n",
        " * [1008] 前序遍历构造二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    // 存储 inorder 中值到索引的映射\n",
        "    HashMap<Integer, Integer> valToIndex = new HashMap<>();\n",
        "\n",
        "    public TreeNode bstFromPreorder(int[] preorder) {\n",
        "        //中序是升序\n",
        "        int len = preorder.length;\n",
        "         int[] inorder = new int[len];\n",
        "        System.arraycopy(preorder, 0, inorder, 0, len);\n",
        "        Arrays.sort(inorder);\n",
        "\n",
        "        int index = 0;\n",
        "        for (Integer value : inorder) {\n",
        "            valToIndex.put(value, index);\n",
        "            index++;\n",
        "        }\n",
        "        \n",
        "        return build(preorder,0,preorder.length-1,\n",
        "                inorder,0,inorder.length-1); \n",
        "\n",
        "    }\n",
        "\n",
        "    TreeNode build(int[] preorder, int preStart, int preEnd, \n",
        "               int[] inorder, int inStart, int inEnd) {\n",
        "\n",
        "    if (preStart > preEnd) {\n",
        "        return null;\n",
        "    }\n",
        "\n",
        "    // root 节点对应的值就是前序遍历数组的第一个元素\n",
        "    int rootVal = preorder[preStart];\n",
        "    // rootVal 在中序遍历数组中的索引\n",
        "    int index = valToIndex.get(rootVal);\n",
        "\n",
        "    int leftSize = index - inStart;\n",
        "\n",
        "    // 先构造出当前根节点\n",
        "    TreeNode root = new TreeNode(rootVal);\n",
        "    // 递归构造左右子树\n",
        "    root.left = build(preorder, preStart + 1, preStart + leftSize,\n",
        "                      inorder, inStart, index - 1);\n",
        "\n",
        "    root.right = build(preorder, preStart + leftSize + 1, preEnd,\n",
        "                       inorder, index + 1, inEnd);\n",
        "    return root;\n",
        "}\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4f06d23c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=100 lang=java\n",
        " *\n",
        " * [100] 相同的树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "    public boolean isSameTree(TreeNode p, TreeNode q) {\n",
        "           return  traverse( p, q);\n",
        "    }\n",
        "\n",
        "    boolean traverse(TreeNode p,TreeNode q){\n",
        "        //base case\n",
        "        if(q == null && p == null) return true;\n",
        "\n",
        "        if(q == null || p == null) return false;\n",
        "\n",
        "       \n",
        "         \n",
        "        boolean left  = traverse(p.left,q.left);\n",
        "        boolean right  = traverse(p.right,q.right);\n",
        "\n",
        "        return  (p.val == q.val) && left && right;\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5446d372",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1768 lang=java\n",
        " *\n",
        " * [1768] 交替合并字符串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String mergeAlternately(String word1, String word2) {\n",
        "        int m = word1.length(),n = word2.length();\n",
        "        int i = 0,j = 0;\n",
        "\n",
        "        StringBuilder ans = new StringBuilder();\n",
        "        while(i < m || j < n){\n",
        "            if(i<m){\n",
        "                ans.append(word1.charAt(i));\n",
        "                ++i;\n",
        "            }\n",
        "            if(j < n){\n",
        "                ans.append(word2.charAt(j));\n",
        "                ++j;\n",
        "            }\n",
        "        }\n",
        "        return ans.toString();\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9762ab36",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=79 lang=java\n",
        " *\n",
        " * [79] 单词搜索\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean exist(char[][] board, String word) {\n",
        "        char[] words =word.toCharArray();\n",
        "        for(int i=0;i<board.length;i++){\n",
        "            for(int j=0;j<board[0].length;j++){\n",
        "                if(dfs(board,words,i,j,0)) return true;\n",
        "            }\n",
        "        }\n",
        "        return false;\n",
        "    }\n",
        "    boolean dfs(char[][] board,char[] word,int i,int j,int k){\n",
        "        if(i>=board.length \n",
        "        || i <0 \n",
        "        ||j>=board[0].length \n",
        "        || j < 0 \n",
        "        || board[i][j] !=word[k])\n",
        "            return false;\n",
        "        if(k == word.length -1) return true;\n",
        "        //修改空字符串\n",
        "        board[i][j] = '\\0';\n",
        "\n",
        "        boolean res = \n",
        "        dfs(board,word,i+1,j,k+1) \n",
        "        || dfs(board,word,i-1,j,k+1)\n",
        "        ||dfs(board,word,i,j+1,k+1) \n",
        "        || dfs(board,word,i,j-1,k+1);\n",
        "        \n",
        "        //还原\n",
        "        board[i][j]  =word[k];\n",
        "        return res;\n",
        "    }\n",
        "} \n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d29805a1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=18 lang=java\n",
        " * @lcpr version=20003\n",
        " *\n",
        " * [18] 四数之和\n",
        " */\n",
        "\n",
        "\n",
        "// @lcpr-template-start\n",
        "\n",
        "// @lcpr-template-end\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<Integer>> fourSum(int[] nums, int target) {\n",
        "        Arrays.sort(nums);\n",
        "        // n 为 4，从 nums[0] 开始计算和为 target 的四元组\n",
        "        return nSumTarget(nums, 4, 0, target);\n",
        "    }\n",
        "\n",
        "    // 注意：调用这个函数之前一定要先给 nums 排序\n",
        "    // n 填写想求的是几数之和，start 从哪个索引开始计算（一般填 0），target 填想凑出的目标和\n",
        "    private List<List<Integer>> nSumTarget(int[] nums, int n, int start, long target) {\n",
        "        int sz = nums.length;\n",
        "        List<List<Integer>> res = new ArrayList<>();\n",
        "        // 至少是 2Sum，且数组大小不应该小于 n\n",
        "        if (n < 2 || sz < n) return res;\n",
        "        // 2Sum 是 base case\n",
        "        if (n == 2) {\n",
        "            // 双指针那一套操作\n",
        "            int lo = start, hi = sz - 1;\n",
        "            while (lo < hi) {\n",
        "                int sum = nums[lo] + nums[hi];\n",
        "                int left = nums[lo], right = nums[hi];\n",
        "                if (sum < target) {\n",
        "                    while (lo < hi && nums[lo] == left) lo++;\n",
        "                } else if (sum > target) {\n",
        "                    while (lo < hi && nums[hi] == right) hi--;\n",
        "                } else {\n",
        "                    res.add(new ArrayList<>(Arrays.asList(left, right)));\n",
        "                    while (lo < hi && nums[lo] == left) lo++;\n",
        "                    while (lo < hi && nums[hi] == right) hi--;\n",
        "                }\n",
        "            }\n",
        "        } else {\n",
        "            // n > 2 时，递归计算 (n-1)Sum 的结果\n",
        "            for (int i = start; i < sz; i++) {\n",
        "                List<List<Integer>> sub = nSumTarget(nums, n - 1, i + 1, target - nums[i]);\n",
        "                for (List<Integer> arr : sub) {\n",
        "                    // (n-1)Sum 加上 nums[i] 就是 nSum\n",
        "                    arr.add(nums[i]);\n",
        "                    res.add(arr);\n",
        "                }\n",
        "                while (i < sz - 1 && nums[i] == nums[i + 1]) i++;\n",
        "            }\n",
        "        }\n",
        "        return res;\n",
        "}\n",
        "}\n",
        "// @lc code=end\n",
        "\n",
        "\n",
        "\n",
        "/*\n",
        "// @lcpr case=start\n",
        "// [1,0,-1,0,-2,2]\\n0\\n\n",
        "// @lcpr case=end\n",
        "\n",
        "// @lcpr case=start\n",
        "// [2,2,2,2,2]\\n8\\n\n",
        "// @lcpr case=end\n",
        "\n",
        " */\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e67faaf0",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=889 lang=java\n",
        " *\n",
        " * [889] 根据前序和后序遍历构造二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "    // 存储 postorder 中值到索引的映射\n",
        "    HashMap<Integer, Integer> valToIndex = new HashMap<>();\n",
        "\n",
        "    public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {\n",
        "        for (int i = 0; i < postorder.length; i++) {\n",
        "            valToIndex.put(postorder[i], i);\n",
        "        }\n",
        "        return build(preorder, 0, preorder.length - 1,\n",
        "                    postorder, 0, postorder.length - 1);\n",
        "\n",
        "    }\n",
        "\n",
        "    // 定义：根据 preorder[preStart..preEnd] 和 postorder[postStart..postEnd]\n",
        "    // 构建二叉树，并返回根节点。\n",
        "    TreeNode build(int[] preorder, int preStart, int preEnd,\n",
        "                   int[] postorder, int postStart, int postEnd) {\n",
        "        if (preStart > preEnd) {\n",
        "            return null;\n",
        "        }\n",
        "        if (preStart == preEnd) {\n",
        "            return new TreeNode(preorder[preStart]);\n",
        "        }\n",
        "\n",
        "        // root 节点对应的值就是前序遍历数组的第一个元素\n",
        "        int rootVal = preorder[preStart];\n",
        "        // root.left 的值是前序遍历第二个元素\n",
        "        // 通过前序和后序遍历构造二叉树的关键在于通过左子树的根节点\n",
        "        // 确定 preorder 和 postorder 中左右子树的元素区间\n",
        "        int leftRootVal = preorder[preStart + 1];\n",
        "        // leftRootVal 在后序遍历数组中的索引\n",
        "        int index = valToIndex.get(leftRootVal);\n",
        "        // 左子树的元素个数\n",
        "        int leftSize = index - postStart + 1;\n",
        "\n",
        "        // 先构造出当前根节点\n",
        "        TreeNode root = new TreeNode(rootVal);\n",
        "        // 递归构造左右子树\n",
        "        // 根据左子树的根节点索引和元素个数推导左右子树的索引边界\n",
        "        root.left = build(preorder, preStart + 1, preStart + leftSize,\n",
        "                postorder, postStart, index);\n",
        "        root.right = build(preorder, preStart + leftSize + 1, preEnd,\n",
        "                postorder, index + 1, postEnd - 1);\n",
        "\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "45f0e0b5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=56 lang=java\n",
        " *\n",
        " * [56] 合并区间\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[][] merge(int[][] intervals) {\n",
        "        \n",
        "        if(intervals.length == 0){\n",
        "            return new int[0][2];\n",
        "        }\n",
        "        //先排序\n",
        "        Arrays.sort(intervals,new Comparator<int[]>(){\n",
        "            public int compare(int[] interval1,int[] interval2){\n",
        "                return interval1[0] - interval2[0];\n",
        "            }\n",
        "        });\n",
        "        List<int[]> merged = new ArrayList<int[]>();\n",
        "        for(int i=0;i<intervals.length;++i){\n",
        "            int L = intervals[i][0],R = intervals[i][1];\n",
        "            if(merged.size() == 0 || merged.get(merged.size()-1)[1] < L){\n",
        "                merged.add(new int[]{L,R});\n",
        "            }else{\n",
        "                merged.get(merged.size()-1)[1] = Math.max(merged.get(merged.size()-1)[1],R);\n",
        "            }\n",
        "        }\n",
        "        return merged.toArray(new int[merged.size()][]);\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5a8f9305",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=104 lang=java\n",
        " *\n",
        " * [104] 二叉树的最大深度\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "     int res = 0;\n",
        "     int depth =0;\n",
        "\n",
        "    public int maxDepth(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null){\n",
        "             res = Math.max(depth,res);\n",
        "             return;   \n",
        "        }\n",
        "        //前序位置\n",
        "        depth++;\n",
        "        traverse(root.left);\n",
        "        //中序位置\n",
        "        traverse(root.right);\n",
        "        //后序位置\n",
        "        depth--;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a638d09a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=109 lang=java\n",
        " *\n",
        " * [109] 有序链表转换二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode sortedListToBST(ListNode head) {\n",
        "       \n",
        "       return  build(head);\n",
        "\n",
        "    }\n",
        "\n",
        "    TreeNode build(ListNode head){\n",
        "        if(head == null) return null;\n",
        "\n",
        "        ListNode fast = head;\n",
        "        ListNode slow = head;\n",
        "\n",
        "        ListNode preSlow =null ;\n",
        " \n",
        "        //记录慢指针前一个节点 \n",
        "        while(fast !=null&& fast.next !=null){\n",
        "            preSlow = slow;\n",
        "            slow = slow.next;\n",
        "            fast = fast.next.next;   \n",
        "        }\n",
        "        TreeNode root = new TreeNode(slow.val);\n",
        "        //左子树是否需要构建 看preSlow 是否为空\n",
        "        if(preSlow != null){\n",
        "            preSlow.next = null;\n",
        "            root.left = build(head);\n",
        "        }     \n",
        "        root.right = build(slow.next);\n",
        "       \n",
        "         \n",
        "        \n",
        "        return root;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "983bad85",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2049 lang=java\n",
        " *\n",
        " * [2049] 统计最高分的节点数目\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    // 表示最高得分\n",
        "    long maxScore = 0;\n",
        "    // 记录最高的分的个数\n",
        "    int cnt = 0;\n",
        "    // 记录parents数组的个数\n",
        "    int n;\n",
        "    // 以数组的形式表示树结构\n",
        "    List<Integer>[] children;\n",
        "\n",
        "    public int countHighestScoreNodes(int[] parents) {\n",
        "        n = parents.length;\n",
        "        children = new List[n];\n",
        "        // 初始化children数组\n",
        "        for (int i = 0; i < n; i++) {\n",
        "            children[i] = new ArrayList<Integer>();\n",
        "        }\n",
        "        // 利用parents得出第i个节点子节点为children[i]\n",
        "        // 比如树：\n",
        "        //     0\n",
        "        //    / \\\n",
        "        //   2   4\n",
        "        //  / \\\n",
        "        // 3   1\n",
        "        // 注意这个数字代表的就是它是第几个节点，它的编号\n",
        "        // 以数组形式表示的话就是\n",
        "        // {\n",
        "        //  // 第0个节点的子结点为2和4\n",
        "        //  {2, 4}.\n",
        "        //  // 第1个节点没有子结点\n",
        "        //  {},\n",
        "        //  // 第2个节点的子结点为3和1\n",
        "        //  {3, 1},\n",
        "        //  // 第3个节点没有子结点\n",
        "        //  {},\n",
        "        //  // 第4个子结点没有子节点\n",
        "        //  {}\n",
        "        // }\n",
        "        for (int i = 0; i < n; i++) {\n",
        "            int p = parents[i];\n",
        "            if (p != -1) {\n",
        "                children[p].add(i);\n",
        "            }\n",
        "        }\n",
        "        dfs(0);\n",
        "        return cnt;\n",
        "    }\n",
        "\n",
        "    // 返回值为节点i下所有节点数量\n",
        "    // 顺便计算每个节点的得分\n",
        "    public int dfs(int node) {\n",
        "        // 记录分数\n",
        "        long score = 1;\n",
        "        // 计算以这个节点为根结点的子树移除所形成的子树需要减去当前节点的，所以需要-1\n",
        "        int size = n - 1;\n",
        "        for (int c : children[node]) {\n",
        "            // t就是c集合中子树的节点数量\n",
        "            int t = dfs(c);\n",
        "            // 计算当前节点乘子树个数得到的分数\n",
        "            score *= t;\n",
        "            // 减去所有子树的个数，当前size就是以这个节点为根结点的子树移除所形成的子树的个数了\n",
        "            size -= t;\n",
        "        }\n",
        "        // 如果当前的node是不是第0个节点，那么计算分数的时候需要再乘上以这个节点为根结点的子树移除所形成的子树\n",
        "        if (node != 0) {\n",
        "            // 所以此时当前节点的分数就是score*=size\n",
        "            score *= size;\n",
        "        }\n",
        "        // 如果当前的分数是等于最高的分数的，那么cnt个数就+1\n",
        "        if (score == maxScore) {\n",
        "            cnt++;\n",
        "        } else if (score > maxScore) {\n",
        "            // 如果还有比maxScore大的数，那么将新的最大的分数给maxScore\n",
        "            maxScore = score;\n",
        "            // 并且重置统计的cnt，毕竟最大值都变了，之前统计的也就不算了\n",
        "            cnt = 1;\n",
        "        }\n",
        "        // 计算当前node为根节点的子树的节点数量，包括根节点\n",
        "        return n - size;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1c970b8d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=27 lang=java\n",
        " *\n",
        " * [27] 移除元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int removeElement(int[] nums, int val) {\n",
        "        if(nums.length ==0) return 0;\n",
        "        int slow =0 ,fast =0;\n",
        "        while(fast<nums.length){\n",
        "            if(nums[fast] != val){\n",
        "                nums[slow] = nums[fast];\n",
        "                slow++;\n",
        "\n",
        "            }\n",
        "            fast ++;\n",
        "        }\n",
        "        return slow;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "89425189",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=198 lang=java\n",
        " *\n",
        " * [198] 打家劫舍\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int rob(int[] nums) {\n",
        "        if(nums.length ==0){\n",
        "            return 0;\n",
        "        }\n",
        "        //子问题\n",
        "        \n",
        "\n",
        "        int N = nums.length;\n",
        "        int[] dp = new int[N+1];\n",
        "        dp[0] = 0;\n",
        "        dp[1] = nums[0];\n",
        "        for(int k =2;k<=N;k++){\n",
        "            dp[k] = Math.max(dp[k-1],dp[k-2]+nums[k-1]);\n",
        "        }\n",
        "        return dp[N];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0e42f9aa",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=993 lang=java\n",
        " *\n",
        " * [993] 二叉树的堂兄弟节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "    public boolean isCousins(TreeNode root, int x, int y) {\n",
        "        if(root == null) return false;\n",
        "\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "\n",
        "        while(!q.isEmpty()){\n",
        "            int sz = q.size();\n",
        "            TreeNode xNode =null;\n",
        "            TreeNode yNode = null;\n",
        "            TreeNode xfNode =null;\n",
        "            TreeNode yfNode = null;\n",
        "            for(int i =0;i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                if(cur.left!=null){\n",
        "                    q.offer(cur.left);\n",
        "                    if(cur.left.val == x){\n",
        "                          xNode = cur.left;\n",
        "                          xfNode =cur;\n",
        "                    }\n",
        "                    if(cur.left.val == y){\n",
        "                          yNode = cur.left;\n",
        "                          yfNode =cur;\n",
        "                    }\n",
        "                }\n",
        "\n",
        "                 if(cur.right!=null){\n",
        "                      q.offer(cur.right);\n",
        "                    if(cur.right.val == x){\n",
        "                          xNode = cur.right;\n",
        "                          xfNode =cur;\n",
        "                    }\n",
        "                    if(cur.right.val == y){\n",
        "                          yNode = cur.right;\n",
        "                          yfNode =cur;\n",
        "                    }\n",
        "                 }\n",
        "            }\n",
        "            if(xfNode!=null && xNode!=null \n",
        "                && xfNode!=null && yfNode!=null\n",
        "                && xfNode.val!=yfNode.val){\n",
        "                    return true;\n",
        "                }\n",
        "\n",
        "        }\n",
        "        return false;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ba139548",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=113 lang=java\n",
        " *\n",
        " * [113] 路径总和 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "    public List<List<Integer>> res = new ArrayList<>();\n",
        "    public List<Integer> list = new ArrayList<>();\n",
        "    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {\n",
        "\n",
        "        \n",
        "         traverse(root,targetSum);\n",
        "         return res;\n",
        "    }\n",
        "\n",
        "    void  traverse(TreeNode root,int targetSum){\n",
        "        //base case\n",
        "        if(root == null) {return ;}\n",
        "\n",
        "        /** 前序位置 */\n",
        "        list.add(root.val);\n",
        "        \n",
        "        //判断叶子节点\n",
        "        if(root.left == null \n",
        "         && root.right == null\n",
        "         &&(root.val == targetSum) ){\n",
        "             // 需要new ArrayList  接收，因为list 作用域为全局\n",
        "             res.add(new ArrayList(list));\n",
        "        }\n",
        "        /**   */\n",
        "        \n",
        "\n",
        "        traverse(root.left,targetSum - root.val);\n",
        "        traverse(root.right,targetSum - root.val);\n",
        "\n",
        "        list.remove(list.size()-1);\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "        \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7d7c1b9e",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2096 lang=java\n",
        " *\n",
        " * [2096] 从二叉树一个节点到另一个节点每一步的方向\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    StringBuilder res = new StringBuilder();\n",
        "\n",
        "    public String getDirections(TreeNode root, int startValue, int destValue) {\n",
        "        StringBuilder sb = new StringBuilder();\n",
        "        //1.最近公共祖先\n",
        "        TreeNode newRoot = dfs(root, startValue, destValue);\n",
        "        //2.添加start到root的路径\n",
        "        int num = findSrc(newRoot, startValue);\n",
        "        for(int i = 0; i < num; i++){\n",
        "            res.append('U');\n",
        "        }\n",
        "        //3.添加root到dest的路径\n",
        "        findDest(newRoot, destValue, sb);\n",
        "        return res.toString();\n",
        "    }\n",
        "\n",
        "    //寻找最近的公共祖先newRoot\n",
        "    public TreeNode dfs(TreeNode root, int startValue, int destValue){\n",
        "        if(root == null || root.val == startValue || root.val == destValue){\n",
        "            return root;\n",
        "        }\n",
        "        TreeNode l = dfs(root.left, startValue, destValue);\n",
        "        TreeNode r = dfs(root.right, startValue, destValue);\n",
        "        if(l != null && r != null){\n",
        "            return root;\n",
        "        }\n",
        "        if(l == null){\n",
        "            return r;\n",
        "        }\n",
        "        if(r == null){\n",
        "            return l;\n",
        "        }\n",
        "        return null;\n",
        "    }\n",
        "    //寻找newRoot到dest的路径\n",
        "    public void findDest(TreeNode root, int destValue, StringBuilder sb){\n",
        "        if(root.val == destValue){\n",
        "            res.append(sb.toString());\n",
        "            return;\n",
        "        }\n",
        "        if(root.right != null){\n",
        "        sb.append('R');\n",
        "        findDest(root.right, destValue, sb);\n",
        "        sb.deleteCharAt(sb.length() - 1);\n",
        "        }\n",
        "        \n",
        "        if(root.left != null){\n",
        "        sb.append('L');\n",
        "        findDest(root.left, destValue, sb);\n",
        "        sb.deleteCharAt(sb.length() - 1);\n",
        "        }\n",
        "    }\n",
        "    //寻找start到newRoot有几个U\n",
        "    public int findSrc(TreeNode root, int srcValue){\n",
        "        if(root == null){\n",
        "            return 10000;\n",
        "        }\n",
        "        if(root.val == srcValue){\n",
        "            return 0;\n",
        "        }\n",
        "        return Math.min(findSrc(root.left, srcValue), findSrc(root.right, srcValue)) + 1;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5ff53ec0",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2265 lang=java\n",
        " *\n",
        " * [2265] 统计值等于子树平均值的节点数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "  int cnt;\n",
        "\n",
        "  public int averageOfSubtree(TreeNode root) {\n",
        "    dfs(root);\n",
        "    return cnt;\n",
        "  }\n",
        "\n",
        "  private int[] dfs(TreeNode root) {\n",
        "    if (root == null) return new int[]{0, 0};\n",
        "\n",
        "    int[] l = dfs(root.left);\n",
        "    int[] r = dfs(root.right);\n",
        "    int sum = (l[0] + r[0] + root.val), nodeNum = (l[1] + r[1] + 1), avg = sum / nodeNum;\n",
        "    if (avg == root.val) cnt++;\n",
        "    return new int[]{sum, nodeNum};\n",
        "  }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c8d19078",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=257 lang=java\n",
        " *\n",
        " * [257] 二叉树的所有路径\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    List<String> res = new LinkedList<>();\n",
        "    LinkedList<String> linkedList = new LinkedList<>();\n",
        "    public List<String> binaryTreePaths(TreeNode root) {\n",
        "\n",
        "            traverse(root);\n",
        "            return res;\n",
        "    }\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "\n",
        "        /** 前序位置 */\n",
        "        linkedList.add(\"\"+root.val);\n",
        "\n",
        "        //叶子节点\n",
        "        if(root.left == null && root.right == null){\n",
        "            res.add( linkedList.stream().collect(Collectors.joining(\"->\")));\n",
        "        }\n",
        "        traverse(root.left);\n",
        "        traverse(root.right);\n",
        "\n",
        "        /** 后序位置 */\n",
        "        //维护 linkedList\n",
        "       linkedList.removeLast();\n",
        "         \n",
        "        \n",
        "    }  \n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "941887f9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=560 lang=java\n",
        " *\n",
        " * [560] 和为 K 的子数组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int subarraySum(int[] nums, int k) {\n",
        "\n",
        "     \n",
        "        int n = nums.length;\n",
        "        //map:前缀和 -> 该前缀和出现的次数\n",
        "        HashMap<Integer,Integer> preSum = new HashMap<>();\n",
        "        //base case\n",
        "        preSum.put(0,1);\n",
        "\n",
        "        int res = 0,sum0_i =0;\n",
        "        for(int i=0;i<n;i++){\n",
        "            sum0_i += nums[i];\n",
        "            \n",
        "            // 前缀和 nums[0..j]\n",
        "            int sum0_j = sum0_i - k;\n",
        "            if(preSum.containsKey(sum0_j)){\n",
        "                res += preSum.get(sum0_j);\n",
        "            }\n",
        "\n",
        "            preSum.put(sum0_i,preSum.getOrDefault(sum0_i,0) +1);\n",
        "            \n",
        "        }\n",
        "        return res;\n",
        "\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "114fa86c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1143 lang=java\n",
        " *\n",
        " * [1143] 最长公共子序列\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int longestCommonSubsequence(String text1, String text2) {\n",
        "        int m  = text1.length(),n = text2.length();\n",
        "        int[][] dp = new int[m+1][n+1];\n",
        "        for(int i=1;i<=m;i++){\n",
        "            char c1 = text1.charAt(i-1);\n",
        "            for(int j =1;j<=n;j++){\n",
        "                char c2 = text2.charAt(j-1);\n",
        "                if(c1 == c2){\n",
        "                    dp[i][j] = dp[i-1][j-1]+1;\n",
        "                }else{\n",
        "                    dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1]);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return dp[m][n];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d80e2ed1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2390 lang=java\n",
        " *\n",
        " * [2390] 从字符串中移除星号\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String removeStars(String s) {\n",
        "        StringBuffer sb = new StringBuffer();\n",
        "        int length = s.length();\n",
        "        for(int i=0;i<length;i++){\n",
        "            char c = s.charAt(i);\n",
        "            if(c !='*'){\n",
        "                sb.append(c);\n",
        "            }else{\n",
        "                sb.deleteCharAt(sb.length()-1);\n",
        "            }\n",
        "        }\n",
        "        return sb.toString();\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "568bc69c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=543 lang=java\n",
        " *\n",
        " * [543] 二叉树的直径\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int maxDiameter = 0;\n",
        "    public int diameterOfBinaryTree(TreeNode root) {\n",
        "        maxDepth(root);\n",
        "        return maxDiameter;\n",
        "    }\n",
        "\n",
        "    int maxDepth(TreeNode root){\n",
        "        if(root == null){\n",
        "            return 0;\n",
        "        }\n",
        "        int lefMax = maxDepth(root.left);\n",
        "        int rightMax = maxDepth(root.right);\n",
        "\n",
        "        int myDiameter =lefMax+rightMax;\n",
        "        maxDiameter = Math.max(myDiameter,maxDiameter);\n",
        "\n",
        "        return 1+Math.max(lefMax,rightMax);\n",
        "\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2b823f88",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=990 lang=java\n",
        " *\n",
        " * [990] 等式方程的可满足性\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean equationsPossible(String[] equations) {\n",
        "        //26个英文字母\n",
        "        UF uf = new UF(26);\n",
        "        //先让相等的字母形成相通分量\n",
        "        for(String eq : equations){\n",
        "            if(eq.charAt(1) == '='){\n",
        "                char x = eq.charAt(0);\n",
        "                char y = eq.charAt(3);\n",
        "                uf.union(x - 'a',y-'a');\n",
        "            }\n",
        "        }\n",
        "        //检查不等关系是否打破相等关系的连通性\n",
        "        for(String eq : equations){\n",
        "            if(eq.charAt(1) == '!'){\n",
        "                char x = eq.charAt(0);\n",
        "                char y = eq.charAt(3);\n",
        "                if(uf.connected(x- 'a',y-'a')){\n",
        "                    return false;\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return true;\n",
        "    }\n",
        "}\n",
        "class UF {\n",
        "    // 连通分量个数\n",
        "    private int count;\n",
        "    // 存储每个节点的父节点\n",
        "    private int[] parent;\n",
        "\n",
        "    // n 为图中节点的个数\n",
        "    public UF(int n) {\n",
        "        this.count = n;\n",
        "        parent = new int[n];\n",
        "        for (int i = 0; i < n; i++) {\n",
        "            parent[i] = i;\n",
        "        }\n",
        "    }\n",
        "\n",
        "    // 将节点 p 和节点 q 连通\n",
        "    public void union(int p, int q) {\n",
        "        int rootP = find(p);\n",
        "        int rootQ = find(q);\n",
        "\n",
        "        if (rootP == rootQ)\n",
        "            return;\n",
        "\n",
        "        parent[rootQ] = rootP;\n",
        "        // 两个连通分量合并成一个连通分量\n",
        "        count--;\n",
        "    }\n",
        "\n",
        "    // 判断节点 p 和节点 q 是否连通\n",
        "    public boolean connected(int p, int q) {\n",
        "        int rootP = find(p);\n",
        "        int rootQ = find(q);\n",
        "        return rootP == rootQ;\n",
        "    }\n",
        "\n",
        "    public int find(int x) {\n",
        "        //这是关键优化点\n",
        "        if (parent[x] != x) {\n",
        "            parent[x] = find(parent[x]);\n",
        "        }\n",
        "        return parent[x];\n",
        "    }\n",
        "\n",
        "    // 返回图中的连通分量个数\n",
        "    public int count() {\n",
        "        return count;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "040d9c27",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1004 lang=java\n",
        " *\n",
        " * [1004] 最大连续1的个数 III\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int longestOnes(int[] nums, int k) {\n",
        "        int n = nums.length;\n",
        "        int left =0,lsum =0,rsum=0;\n",
        "        int ans =0;\n",
        "        for(int right = 0;right<n;++right){\n",
        "            rsum += 1-nums[right];\n",
        "            while(lsum < rsum-k){\n",
        "                lsum += 1- nums[left];\n",
        "                left++;\n",
        "            }\n",
        "            ans = Math.max(ans,right-left +1);\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3071c7eb",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=226 lang=java\n",
        " *\n",
        " * [226] 翻转二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode invertTree(TreeNode root) {\n",
        "            traverse(root);\n",
        "            return root;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return;\n",
        "\n",
        "        TreeNode temp = root.left;\n",
        "        root.left = root.right;\n",
        "        root.right = temp;\n",
        "\n",
        "        traverse(root.left);\n",
        "        traverse(root.right);\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6a2c8628",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=374 lang=java\n",
        " *\n",
        " * [374] 猜数字大小\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/** \n",
        " * Forward declaration of guess API.\n",
        " * @param  num   your guess\n",
        " * @return \t     -1 if num is higher than the picked number\n",
        " *\t\t\t      1 if num is lower than the picked number\n",
        " *               otherwise return 0\n",
        " * int guess(int num);\n",
        " */\n",
        "\n",
        "public class Solution extends GuessGame {\n",
        "    public int guessNumber(int n) {\n",
        "        int left=1,right =n;\n",
        "        while(left <right){\n",
        "            int mid = left + (right - left) / 2;\n",
        "            if(guess(mid) <=0){\n",
        "                right = mid;\n",
        "            }else{\n",
        "                left = mid +1;\n",
        "            }\n",
        "        }\n",
        "        return left;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "012521be",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=112 lang=java\n",
        " *\n",
        " * [112] 路径总和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean hasPathSum(TreeNode root, int targetSum) {\n",
        "\n",
        "        return  traverse(root,targetSum);\n",
        "\n",
        "    }\n",
        "\n",
        "    boolean  traverse(TreeNode root,int targetSum){\n",
        "        //base case\n",
        "        if(root == null) {return false;}\n",
        "\n",
        "        /** 前序位置 */\n",
        "        \n",
        "        //判断叶子节点\n",
        "        if(root.left == null \n",
        "         && root.right == null\n",
        "         &&(root.val == targetSum) ){\n",
        "            return true;\n",
        "        }\n",
        "        /**   */\n",
        "\n",
        "        boolean left = traverse(root.left,targetSum - root.val);\n",
        "        boolean right = traverse(root.right,targetSum - root.val);\n",
        "\n",
        "        return right||left ;\n",
        "\n",
        "\n",
        "        \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "73d19d31",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=9 lang=java\n",
        " *\n",
        " * [9] 回文数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean isPalindrome(int x) {\n",
        "       //特殊情况:\n",
        "       //如上述所述，当 x < 0 时，x 不是回文数。\n",
        "       //同样地，如果数字的最后一位是 0，为了使该数字为回文，\n",
        "       //则其第一位数字也应该是 0\n",
        "       //只有 0 满足这一属性\n",
        "       if(x<0 || (x%10==0 && x!=0)){\n",
        "           return false;\n",
        "       }\n",
        "       int revertedNumber = 0;\n",
        "       while(x > revertedNumber){\n",
        "           revertedNumber = revertedNumber*10 + x%10;   \n",
        "           x /= 10;\n",
        "       }\n",
        "       //当数字长度为奇数时，我们可以通过 revertedNumber/10 去除处于中位的数字。\n",
        "       //例如，当输入为 12321 时，在 while 循环的末尾我们可以得到 x = 12，revertedNumber = 123，\n",
        "       //由于处于中位的数字不影响回文（它总是与自己相等），所以我们可以简单地将其去除。\n",
        "       \n",
        "       return x == revertedNumber || x == revertedNumber/10;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5f4f306c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=401 lang=java\n",
        " *\n",
        " * [401] 二进制手表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<String> readBinaryWatch(int turnedOn) {\n",
        "        List<String> ans = new ArrayList<String>();\n",
        "        for(int h=0;h<12;h++){\n",
        "            for(int m=0;m<60;m++){\n",
        "                if(Integer.bitCount(h)+Integer.bitCount(m) == turnedOn){\n",
        "                    ans.add(h+\":\"+(m<10?\"0\":\"\")+m);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cfcbea92",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=297 lang=java\n",
        " *\n",
        " * [297] 二叉树的序列化与反序列化\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "public class Codec {\n",
        "    String SEP=\",\";\n",
        "    String NULL = \"null\";\n",
        "    // Encodes a tree to a single string.\n",
        "    public String serialize(TreeNode root) {\n",
        "        StringBuilder sb = new StringBuilder();\n",
        "        serialize(root,sb);\n",
        "        return sb.toString();\n",
        "    }\n",
        "    void serialize(TreeNode root,StringBuilder sb){\n",
        "        if(root == null){\n",
        "            sb.append(NULL).append(SEP);\n",
        "            return;\n",
        "        }\n",
        "        sb.append(root.val).append(SEP);\n",
        "\n",
        "        serialize(root.left,sb);\n",
        "        serialize(root.right,sb);\n",
        "\n",
        "    }\n",
        "\n",
        "\n",
        "\n",
        "    // Decodes your encoded data to tree.\n",
        "    public TreeNode deserialize(String data) {\n",
        "        LinkedList<String> nodes = new LinkedList<>();\n",
        "        for(String s:data.split(SEP)){\n",
        "            nodes.add(s);\n",
        "        }\n",
        "        return deserialize(nodes);\n",
        "        \n",
        "    }\n",
        "    TreeNode deserialize(LinkedList<String> nodes){\n",
        "        if(nodes.isEmpty()) return null;\n",
        "        /*  前序位置 */\n",
        "        //列表最左侧就是根节点\n",
        "        String first = nodes.removeFirst();\n",
        "        if(first.equals(NULL)) return null;\n",
        "        TreeNode root = new TreeNode(Integer.parseInt(first));\n",
        "        /* */\n",
        "        root.left = deserialize(nodes);\n",
        "        root.right=deserialize(nodes);\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "// Your Codec object will be instantiated and called as such:\n",
        "// Codec ser = new Codec();\n",
        "// Codec deser = new Codec();\n",
        "// TreeNode ans = deser.deserialize(ser.serialize(root));\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9f2a4075",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2462 lang=java\n",
        " *\n",
        " * [2462] 雇佣 K 位工人的总代价\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public long totalCost(int[] costs, int k, int candidates) {\n",
        "        PriorityQueue<Integer> q1 = new PriorityQueue<Integer>();\n",
        "        PriorityQueue<Integer> q2 = new PriorityQueue<Integer>();\n",
        "        int l = candidates,n=costs.length,r =Math.max(l-1,n-candidates-1);\n",
        "        long ans = 0;\n",
        "        for(int i=0;i<candidates;i++) q1.add(costs[i]);\n",
        "        for(int i=n-1;i>r;i--) q2.add(costs[i]);\n",
        "        while(k-->0){\n",
        "            if(!q1.isEmpty()&&!q2.isEmpty()){\n",
        "                if(q1.peek()<=q2.peek()){\n",
        "                    ans +=q1.poll();\n",
        "                    if(l<=r) q1.add(costs[l++]);\n",
        "                }else{\n",
        "                    ans += q2.poll();\n",
        "                    if(l<=r) q2.add(costs[r--]);\n",
        "                }\n",
        "            }else if(q2.isEmpty()){\n",
        "                ans += q1.poll();\n",
        "                if(l<=r) q1.add(costs[l++]);\n",
        "            }else if(q1.isEmpty()){\n",
        "                ans +=q2.poll();\n",
        "                if(l<=r) q2.add(costs[r--]);\n",
        "            }\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2a00d976",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=92 lang=java\n",
        " *\n",
        " * [92] 反转链表 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    ListNode successor = null; //后驱节点\n",
        "\n",
        "    ListNode reverseN(ListNode head,int n){\n",
        "        if(n ==1){\n",
        "            //记录第n+1 节点\n",
        "            successor = head.next;\n",
        "            return head;\n",
        "        }\n",
        "        //head.next为起点，需要反转前n -1 个节点\n",
        "        ListNode last = reverseN(head.next,n-1);\n",
        "        head.next.next = head;\n",
        "        //让反转之后的head 节点和后面的节点连起来\n",
        "        head.next = successor;\n",
        "        return last;\n",
        "\n",
        "\n",
        "    }\n",
        "\n",
        "    public ListNode reverseBetween(ListNode head, int left, int right) {\n",
        "        if(left == 1){\n",
        "            return reverseN(head,right);\n",
        "        }\n",
        "        head.next = reverseBetween(head.next,left -1,right -1);\n",
        "        return head;\n",
        "    }\n",
        "\n",
        "\n",
        "    \n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "dedae059",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=107 lang=java\n",
        " *\n",
        " * [107] 二叉树的层序遍历 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public List<List<Integer>> levelOrderBottom(TreeNode root) {\n",
        "        LinkedList<List<Integer>> res = new LinkedList<>();\n",
        "        traverse( root,res);\n",
        "        return res;\n",
        "\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root,LinkedList<List<Integer>> res){\n",
        "        if( root == null) return;\n",
        "\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "\n",
        "        q.offer(root);\n",
        "\n",
        "        while(!q.isEmpty()){\n",
        "            int sz = q.size();\n",
        "            List<Integer> levelList = new ArrayList<>();\n",
        "            for(int i=0;i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                levelList.add(cur.val);\n",
        "\n",
        "                if(cur.left !=null){\n",
        "                    q.offer(cur.left);\n",
        "                }\n",
        "                 if(cur.right !=null){\n",
        "                    q.offer(cur.right);\n",
        "                }\n",
        "\n",
        "            }\n",
        "            res.addFirst(levelList);\n",
        "        }\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e3726c17",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=392 lang=java\n",
        " *\n",
        " * [392] 判断子序列\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean isSubsequence(String s, String t) {\n",
        "        int n = s.length(),m = t.length();\n",
        "        int i = 0 ,j = 0;\n",
        "        while(i< n && j < m){\n",
        "            if(s.charAt(i) == t.charAt(j)){\n",
        "                i++;\n",
        "            }\n",
        "            j++;\n",
        "        }\n",
        "        return i == n;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "46c78170",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=617 lang=java\n",
        " *\n",
        " * [617] 合并二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\tpublic TreeNode mergeTrees(TreeNode t1, TreeNode t2) {\n",
        "\t\tif(t1==null || t2==null) {\n",
        "\t\t\treturn t1==null? t2 : t1;\n",
        "\t\t}\n",
        "\t\treturn dfs(t1,t2);\n",
        "\t}\n",
        "\t\n",
        "\tTreeNode dfs(TreeNode r1, TreeNode r2) {\n",
        "\t\t// 如果 r1和r2中，只要有一个是null，函数就直接返回\n",
        "\t\tif(r1==null || r2==null) {\n",
        "\t\t\treturn r1==null? r2 : r1;\n",
        "\t\t}\n",
        "\t\t//让r1的值 等于  r1和r2的值累加，再递归的计算两颗树的左节点、右节点\n",
        "\t\tr1.val += r2.val;\n",
        "\t\tr1.left = dfs(r1.left,r2.left);\n",
        "\t\tr1.right = dfs(r1.right,r2.right);\n",
        "\t\treturn r1;\n",
        "\t}\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f16f7b6b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=399 lang=java\n",
        " *\n",
        " * [399] 除法求值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {\n",
        "        int equationsSize = equations.size();\n",
        "\n",
        "        UnionFind unionFind = new UnionFind(2* equationsSize);\n",
        "        //第一步 预处理 将变量的值与id 进行映射，使得并差集的底层使用数组实现，方便编码\n",
        "        Map<String,Integer> hashMap = new HashMap<>(2* equationsSize);\n",
        "        int id =0;\n",
        "        for(int i=0;i<equationsSize;i++){\n",
        "            List<String> equation = equations.get(i);\n",
        "            String var1 = equation.get(0);\n",
        "            String var2 = equation.get(1);\n",
        "\n",
        "            if(!hashMap.containsKey(var1)){\n",
        "                hashMap.put(var1,id);\n",
        "                ++id;\n",
        "            }\n",
        "            if(!hashMap.containsKey(var2)){\n",
        "                hashMap.put(var2,id);\n",
        "                ++id;\n",
        "            }\n",
        "            unionFind.union(hashMap.get(var1),hashMap.get(var2),values[i]);\n",
        "\n",
        "        }\n",
        "        //第二步 做查询\n",
        "        int queriesSize = queries.size();\n",
        "        double[] res = new double[queriesSize];\n",
        "        for(int i=0;i<queriesSize;i++){\n",
        "            String var1 =queries.get(i).get(0);\n",
        "            String var2 =queries.get(i).get(1);\n",
        "\n",
        "            Integer id1 = hashMap.get(var1);\n",
        "            Integer id2 = hashMap.get(var2);\n",
        "\n",
        "            if(id1 == null || id2 == null){\n",
        "                res[i] = -1.0d;\n",
        "            }else{\n",
        "                res[i] = unionFind.isConnected(id1,id2);\n",
        "            }\n",
        "        }\n",
        "        return  res;\n",
        "\n",
        "    }\n",
        "\n",
        "    private class UnionFind{\n",
        "        private int[] parent;\n",
        "\n",
        "        // 指向的父节点的权值\n",
        "        private double[] weight;\n",
        "\n",
        "        public UnionFind(int n){\n",
        "            this.parent = new int[n];\n",
        "            this.weight = new double[n];\n",
        "            for(int i=0;i<n;i++){\n",
        "                parent[i] = i;\n",
        "                weight[i] = 1.0d;\n",
        "            }\n",
        "        }\n",
        "        public void union(int x,int y,double value){\n",
        "            int rootX = find(x);\n",
        "            int rootY = find(y);\n",
        "            if(rootX == rootY){\n",
        "                return;\n",
        "            }\n",
        "            parent[rootX] =rootY;\n",
        "            //关系式的推导\n",
        "            weight[rootX] = weight[y] * value /weight[x];\n",
        "        }\n",
        "        //路径压缩\n",
        "\n",
        "        public int find(int x){\n",
        "            if(x != parent[x]){\n",
        "                int origin = parent[x];\n",
        "                parent[x] =find(parent[x]);\n",
        "                weight[x] *= weight[origin];\n",
        "            }\n",
        "            return parent[x];\n",
        "        }\n",
        "\n",
        "        public double isConnected(int x,int y){\n",
        "            int rootX = find(x);\n",
        "            int rootY = find(y);\n",
        "            if(rootX == rootY){\n",
        "                return weight[x]/weight[y];\n",
        "            }else{\n",
        "                return -1.0d;\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e6646482",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=331 lang=java\n",
        " *\n",
        " * [331] 验证二叉树的前序序列化\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean isValidSerialization(String preorder) {\n",
        "\n",
        "        String[] str = preorder.split(\",\");\n",
        "        int diff =1;\n",
        "        for(String s:str){\n",
        "            diff --;\n",
        "            if(diff <0) return false;\n",
        "            if(!s.equals(\"#\")) diff+=2;\n",
        "        }\n",
        "        return diff ==0;\n",
        "\n",
        "    }\n",
        "   \n",
        "\n",
        "   \n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "77241b39",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1123 lang=java\n",
        " *\n",
        " * [1123] 最深叶节点的最近公共祖先\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode lcaDeepestLeaves(TreeNode root) {\n",
        "       if(root==null) return null;\n",
        "       int left=dfs(root.left);\n",
        "       int right=dfs(root.right);\n",
        "\n",
        "       // 后序位置\n",
        "       if(left==right){\n",
        "            return root;\n",
        "       }else if(left<right){\n",
        "           return lcaDeepestLeaves(root.right);\n",
        "       } \n",
        "       return lcaDeepestLeaves(root.left);\n",
        "    }\n",
        "    int dfs(TreeNode  node){\n",
        "      if(node==null) return 0;\n",
        "      return 1+Math.max(dfs(node.right),dfs(node.left));\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "322be5f1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=21 lang=java\n",
        " *\n",
        " * [21] 合并两个有序链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {\n",
        "\n",
        "\n",
        "      ListNode p1 =list1;\n",
        "      ListNode p2 =list2;\n",
        "      ListNode dummy =new ListNode(-1),p=dummy;\n",
        "      while(p1!=null && p2!=null){\n",
        "          if(p1.val > p2.val){\n",
        "              p.next = p2;\n",
        "              p2 = p2.next;\n",
        "          }else{\n",
        "              p.next = p1;\n",
        "              p1 =p1.next;  \n",
        "          }\n",
        "          p = p.next;\n",
        "\n",
        "      }\n",
        "       if(p1!=null){\n",
        "           p.next = p1;\n",
        "       }\n",
        "       if(p2!=null){\n",
        "           p.next =p2;\n",
        "       }\n",
        "\n",
        "      return dummy.next;\n",
        "\n",
        "\n",
        "    \n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9f8412b8",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=988 lang=java\n",
        " *\n",
        " * [988] 从叶结点开始的最小字符串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    String ans = \"~\";\n",
        "    public String smallestFromLeaf(TreeNode root) {\n",
        "        dfs(root, new StringBuilder());\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public void dfs(TreeNode node, StringBuilder sb) {\n",
        "        if (node == null) return;\n",
        "        sb.append((char)('a' + node.val));\n",
        "        if (node.left == null && node.right == null) {\n",
        "            sb.reverse();\n",
        "            String S = sb.toString();\n",
        "            sb.reverse();\n",
        "\n",
        "            if (S.compareTo(ans) < 0)\n",
        "                ans = S;\n",
        "        }\n",
        "\n",
        "        dfs(node.left, sb);\n",
        "        dfs(node.right, sb);\n",
        "        \n",
        "        sb.deleteCharAt(sb.length() - 1);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8ed1a16d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=78 lang=java\n",
        " *\n",
        " * [78] 子集\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "\n",
        "    List<Integer> t = new ArrayList<Integer>();\n",
        "    List<List<Integer>> ans = new ArrayList<List<Integer>>();\n",
        "\n",
        "    public List<List<Integer>> subsets(int[] nums) {\n",
        "        int n = nums.length;\n",
        "        for(int mask =0;mask < (1<<n);++mask){\n",
        "            t.clear();\n",
        "            for(int i =0;i<n;++i){\n",
        "                if((mask & (1<<i)) !=0){\n",
        "                    t.add(nums[i]);\n",
        "                }\n",
        "            }\n",
        "            ans.add(new ArrayList<Integer>(t));\n",
        "        }    \n",
        "        return  ans;\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "11374fa8",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=73 lang=java\n",
        " *\n",
        " * [73] 矩阵置零\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public void setZeroes(int[][] matrix) {\n",
        "        int m = matrix.length, n = matrix[0].length;\n",
        "        boolean[] row = new boolean[m];\n",
        "        boolean[] col = new boolean[n];\n",
        "        for(int i=0;i<m;i++){\n",
        "            for(int j=0;j<n;j++){\n",
        "                if(matrix[i][j] == 0){\n",
        "                    row[i] = col[j] = true;\n",
        "                   \n",
        "                }\n",
        "            }\n",
        "        }\n",
        "\n",
        "         for(int i=0;i<m;i++){\n",
        "            for(int j=0;j<n;j++){\n",
        "               if(row[i] || col[j]){\n",
        "                   matrix[i][j] =0;\n",
        "               }\n",
        "            }\n",
        "        }\n",
        "\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bd7d7534",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1305 lang=java\n",
        " *\n",
        " * [1305] 两棵二叉搜索树中的所有元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int INF = 0x3f3f3f3f;\n",
        "    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {\n",
        "        List<Integer> ans = new ArrayList<>();\n",
        "        List<Integer> l1 = new ArrayList<>(), l2 = new ArrayList<>();\n",
        "        dfs(root1, l1); dfs(root2, l2);\n",
        "        int n = l1.size(), m = l2.size(), i = 0, j = 0;\n",
        "        //归并排序\n",
        "        while (i < n || j < m) {\n",
        "            int a = i < n ? l1.get(i) : INF;\n",
        "            int b = j < m ? l2.get(j) : INF;\n",
        "            if (a <= b) {\n",
        "                ans.add(a); i++;\n",
        "            } else {\n",
        "                ans.add(b); j++;\n",
        "            }\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "    void dfs(TreeNode root, List<Integer> list) {\n",
        "        if (root == null) return ;\n",
        "        dfs(root.left, list);\n",
        "        list.add(root.val);\n",
        "        dfs(root.right, list);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f7752c39",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2215 lang=java\n",
        " *\n",
        " * [2215] 找出两数组的不同\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {\n",
        "        HashSet<Integer> set1 = new HashSet<>();\n",
        "        HashSet<Integer> set2 = new HashSet<>();\n",
        "        for (int n : nums2) {\n",
        "            set2.add(n);\n",
        "        }\n",
        "        for (int n : nums1) {\n",
        "            set1.add(n);\n",
        "        }\n",
        "        for (int n : nums2) {\n",
        "            set1.remove(n);\n",
        "        }\n",
        "        for (int n : nums1) {\n",
        "            set2.remove(n);\n",
        "        }\n",
        "\n",
        "        return List.of(List.copyOf(set1), List.copyOf(set2));\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9f098e50",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=106 lang=java\n",
        " *\n",
        " * [106] 从中序与后序遍历序列构造二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "    // 存储 inorder 中值到索引的映射\n",
        "    HashMap<Integer, Integer> valToIndex = new HashMap<>();\n",
        "    \n",
        "    public TreeNode buildTree(int[] inorder, int[] postorder) {\n",
        "        for (int i = 0; i < inorder.length; i++) {\n",
        "            valToIndex.put(inorder[i], i);\n",
        "        }\n",
        "        return build(inorder, 0, inorder.length - 1,\n",
        "                    postorder, 0, postorder.length - 1);\n",
        "    }\n",
        "\n",
        "\n",
        "    TreeNode build(int[] inorder, int inStart, int inEnd,\n",
        "               int[] postorder, int postStart, int postEnd) {\n",
        "\n",
        "    if (inStart > inEnd) {\n",
        "        return null;\n",
        "    }\n",
        "    // root 节点对应的值就是后序遍历数组的最后一个元素\n",
        "    int rootVal = postorder[postEnd];\n",
        "    // rootVal 在中序遍历数组中的索引\n",
        "    int index = valToIndex.get(rootVal);\n",
        "    // 左子树的节点个数\n",
        "    int leftSize = index - inStart;\n",
        "    TreeNode root = new TreeNode(rootVal);\n",
        "    // 递归构造左右子树\n",
        "    root.left = build(inorder, inStart, index - 1,\n",
        "                        postorder, postStart, postStart + leftSize - 1);\n",
        "\n",
        "    root.right = build(inorder, index + 1, inEnd,\n",
        "                        postorder, postStart + leftSize, postEnd - 1);\n",
        "    return root;\n",
        "}\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "81488acc",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=287 lang=java\n",
        " *\n",
        " * [287] 寻找重复数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int findDuplicate(int[] nums) {\n",
        "        //快慢指针\n",
        "        int slow = 0;\n",
        "        int fast = 0;\n",
        "        slow = nums[slow];\n",
        "        fast = nums[nums[fast]];\n",
        "        while(slow != fast){\n",
        "            slow = nums[slow];\n",
        "            fast = nums[nums[fast]];\n",
        "        }\n",
        "        int pre1 = 0;\n",
        "        int pre2 = slow;\n",
        "        while(pre1 != pre2){\n",
        "            pre1 = nums[pre1];\n",
        "            pre2 = nums[pre2];\n",
        "        }\n",
        "        return pre1;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "bb2aad19",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=971 lang=java\n",
        " *\n",
        " * [971] 翻转二叉树以匹配先序遍历\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int i = 0;\n",
        "    List<Integer> res = new ArrayList<>();\n",
        "    public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {\n",
        "        if (dfs(root, voyage)) {\n",
        "            return res;\n",
        "        }\n",
        "        return Arrays.asList(-1);\n",
        "    }\n",
        "\n",
        "    boolean dfs(TreeNode root, int[] v) {\n",
        "        //base case 1\n",
        "        if (root == null) return true;\n",
        "        //base case 2\n",
        "        if (root.val != v[i]) return false;\n",
        "       \n",
        "        i++;\n",
        "        \n",
        "        if (root.left != null && root.left.val != v[i]) {\n",
        "            res.add(root.val);\n",
        "            return dfs(root.right, v) && dfs(root.left, v);\n",
        "        }\n",
        "        return dfs(root.left, v) &&dfs(root.right, v);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ccf4250a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=167 lang=java\n",
        " *\n",
        " * [167] 两数之和 II - 输入有序数组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] twoSum(int[] numbers, int target) {\n",
        "       int left =0,right=numbers.length-1;\n",
        "       while(left <right){\n",
        "           int sum = numbers[left]+numbers[right];\n",
        "           if(sum == target){\n",
        "               return new int[]{left+1,right+1};\n",
        "           }else if(sum > target){\n",
        "               right--;\n",
        "           }else if(sum < target){\n",
        "               left ++;\n",
        "           }\n",
        "       } \n",
        "       return new int[]{-1,-1};\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f3280bef",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=95 lang=java\n",
        " *\n",
        " * [95] 不同的二叉搜索树 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public List<TreeNode> generateTrees(int n) {\n",
        "        if(n ==0) return new LinkedList<>();\n",
        "        return build(1,n);\n",
        "    }\n",
        "    List<TreeNode> build(int lo,int hi){\n",
        "        List<TreeNode> res = new LinkedList<>();\n",
        "        //base case\n",
        "        if(lo > hi){\n",
        "            res.add(null);\n",
        "            return res;\n",
        "        }\n",
        "        //1.穷举root 节点的所有可能\n",
        "        for(int i=lo;i<=hi;i++){\n",
        "            //2、递归构造出左右子树的所有合法BST\n",
        "            List<TreeNode> leftTree = build(lo,i-1);\n",
        "            List<TreeNode> rightTree = build(i+1,hi);\n",
        "            //3、给root 节点穷举所有左右子树的组合\n",
        "            for(TreeNode left:leftTree){\n",
        "                for(TreeNode right:rightTree){\n",
        "                    //i作为根节点root 的值\n",
        "                    TreeNode root = new TreeNode(i);\n",
        "                    root.left = left;\n",
        "                    root.right = right;\n",
        "                    res.add(root);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3092374d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=341 lang=java\n",
        " *\n",
        " * [341] 扁平化嵌套列表迭代器\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * // This is the interface that allows for creating nested lists.\n",
        " * // You should not implement it, or speculate about its implementation\n",
        " * public interface NestedInteger {\n",
        " *\n",
        " *     // @return true if this NestedInteger holds a single integer, rather than a nested list.\n",
        " *     public boolean isInteger();\n",
        " *\n",
        " *     // @return the single integer that this NestedInteger holds, if it holds a single integer\n",
        " *     // Return null if this NestedInteger holds a nested list\n",
        " *     public Integer getInteger();\n",
        " *\n",
        " *     // @return the nested list that this NestedInteger holds, if it holds a nested list\n",
        " *     // Return empty list if this NestedInteger holds a single integer\n",
        " *     public List<NestedInteger> getList();\n",
        " * }\n",
        " */\n",
        " // 转换为N叉树\n",
        "public class NestedIterator implements Iterator<Integer> {\n",
        "\n",
        "    private LinkedList<NestedInteger> list;\n",
        "\n",
        "    public NestedIterator(List<NestedInteger> nestedList) {\n",
        "        // 不直接用 nestedList 的引用，是因为不能确定它的底层实现\n",
        "        // 必须保证是 LinkedList，否则下面的 addFirst 会很低效\n",
        "        list = new LinkedList<>(nestedList);\n",
        "    }\n",
        "\n",
        "    @Override\n",
        "    public Integer next() {\n",
        "      // hasNext 方法保证了第一个元素一定是整数类型\n",
        "        return list.remove(0).getInteger();\n",
        "    }\n",
        "\n",
        "    @Override\n",
        "    public boolean hasNext() {\n",
        "        // 循环拆分列表元素，直到列表第一个元素是整数类型\n",
        "        while (!list.isEmpty() && !list.get(0).isInteger()) {\n",
        "            // 当列表开头第一个元素是列表类型时，进入循环\n",
        "            List<NestedInteger> first = list.remove(0).getList();\n",
        "            // 将第一个列表打平并按顺序添加到开头\n",
        "            for (int i = first.size() - 1; i >= 0; i--) {\n",
        "                list.addFirst(first.get(i));\n",
        "            }\n",
        "        }\n",
        "        return !list.isEmpty();\n",
        "    }\n",
        " \n",
        "\n",
        "\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your NestedIterator object will be instantiated and called as such:\n",
        " * NestedIterator i = new NestedIterator(nestedList);\n",
        " * while (i.hasNext()) v[f()] = i.next();\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ee8d5527",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=13 lang=java\n",
        " *\n",
        " * [13] 罗马数字转整数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    Map<Character,Integer> symbolValues = new HashMap<Character,Integer>(){{\n",
        "        put('I',1);\n",
        "        put('V',5);\n",
        "        put('X',10);\n",
        "        put('L',50);\n",
        "        put('C',100);\n",
        "        put('D',500);\n",
        "        put('M',1000);\n",
        "\n",
        "    }};\n",
        "    public int romanToInt(String s) {\n",
        "        int ans =0;\n",
        "        int n = s.length();\n",
        "        for(int i=0;i<n;++i){\n",
        "            int value = symbolValues.get(s.charAt(i));\n",
        "            if(i<n-1&&value<symbolValues.get(s.charAt(i+1))){\n",
        "                ans -= value;\n",
        "            }else{\n",
        "                ans += value;\n",
        "            }\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6877350b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1382 lang=java\n",
        " *\n",
        " * [1382] 将二叉搜索树变平衡\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    ArrayList<TreeNode> orderList = null;\n",
        "    TreeNode[] orderNode = null;\n",
        "    public TreeNode balanceBST(TreeNode root) {\n",
        "        orderList = new ArrayList<>();\n",
        "\n",
        "        // 思路：先找出所有节点，然后中分取数\n",
        "        // 先找所有节点，中序遍历，查出有序的集合\n",
        "        doSelect(root);\n",
        "\n",
        "        if (orderList.size() < 3) return root; // 个数小于3就没必要了\n",
        "\n",
        "        orderNode = orderList.toArray(new TreeNode[orderList.size()]); // 构建成数组，以便于取区间的中位数\n",
        "\n",
        "        return doBuild(0, orderNode.length - 1); // 构建新的树\n",
        "    }\n",
        "\n",
        "    private TreeNode doBuild(int left, int right){\n",
        "        if (left > right) return null;\n",
        "\n",
        "        int curRootIndex = (left + right) / 2;\n",
        "        TreeNode curRoot = orderNode[curRootIndex];\n",
        "        curRoot.left = doBuild(left, curRootIndex - 1);\n",
        "        curRoot.right = doBuild(curRootIndex + 1, right);\n",
        "        return curRoot;\n",
        "    }\n",
        "\n",
        "    // 中序遍历获取数组链表\n",
        "    private void doSelect(TreeNode curRoot){\n",
        "        if (curRoot == null) return;\n",
        "\n",
        "        doSelect(curRoot.left);\n",
        "        orderList.add(curRoot);\n",
        "        doSelect(curRoot.right);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4a3fa831",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=416 lang=java\n",
        " *\n",
        " * [416] 分割等和子集\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean canPartition(int[] nums) {\n",
        "        int n = nums.length;\n",
        "        if(n < 2){\n",
        "            return false;\n",
        "        }\n",
        "        int sum =0 ,maxNum =0;\n",
        "        for(int num:nums){\n",
        "            sum+= num;\n",
        "            maxNum = Math.max(maxNum,num);\n",
        "        }\n",
        "        if(sum % 2 !=0){\n",
        "            return false;\n",
        "        }\n",
        "        int target = sum / 2;\n",
        "        if(maxNum > target){\n",
        "            return false;\n",
        "        }\n",
        "        boolean[] dp = new boolean[target + 1];\n",
        "        dp[0] = true;\n",
        "        for(int i=0;i<n;i++){\n",
        "            int num = nums[i];\n",
        "            for(int j = target;j>=num;--j){\n",
        "                dp[j] |= dp[j - num];\n",
        "            }\n",
        "        }\n",
        "        return dp[target];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d6463bf1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=3 lang=java\n",
        " *\n",
        " * [3] 无重复字符的最长子串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int lengthOfLongestSubstring(String s) {\n",
        "        if(s.length() ==0) return 0;\n",
        "        HashMap<Character,Integer> map = new HashMap<Character,Integer>();\n",
        "        int max =0;\n",
        "        int left =0;\n",
        "        for(int i=0;i<s.length();i++){\n",
        "            if(map.containsKey(s.charAt(i))){\n",
        "                left = Math.max(left,map.get(s.charAt(i)) +1);\n",
        "            }\n",
        "            map.put(s.charAt(i),i);\n",
        "            max =Math.max(max,i-left +1);\n",
        "        }\n",
        "        return max;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "df516a7a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=345 lang=java\n",
        " *\n",
        " * [345] 反转字符串中的元音字母\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String reverseVowels(String s) {\n",
        "        //双指针\n",
        "        int n = s.length();\n",
        "        char[] arr = s.toCharArray();\n",
        "        int i= 0,j = n -1;\n",
        "        while(i < j){\n",
        "            while(i < n && !isVowel(arr[i])){\n",
        "                ++i;\n",
        "            }\n",
        "            while(j>0 &&!isVowel(arr[j])){\n",
        "                --j;\n",
        "            }\n",
        "            if(i < j){\n",
        "                swap(arr,i,j);\n",
        "                ++i;\n",
        "                --j;\n",
        "            }\n",
        "        }\n",
        "        return new String(arr);\n",
        "\n",
        "    }\n",
        "    public boolean isVowel(char ch){\n",
        "        return \"aeiouAEIOU\".indexOf(ch) >=0;\n",
        "    }\n",
        "    public void swap(char[] arr,int i,int j){\n",
        "        char temp = arr[i];\n",
        "        arr[i] = arr[j];\n",
        "        arr[j] = temp;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c1d4ae7a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=35 lang=java\n",
        " *\n",
        " * [35] 搜索插入位置\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int searchInsert(int[] nums, int target) {\n",
        "        int n = nums.length;\n",
        "        int left = 0, right = n -1,ans = n;\n",
        "        while(left <= right){\n",
        "            int mid = ((right - left) >> 1) + left;\n",
        "            if(target <= nums[mid]){\n",
        "                ans = mid;\n",
        "                right = mid -1;\n",
        "            }else{\n",
        "                left =mid +1;\n",
        "            }\n",
        "        }\n",
        "        return ans;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1b04dbdb",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=606 lang=java\n",
        " *\n",
        " * [606] 根据二叉树创建字符串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public String tree2str(TreeNode root) {\n",
        "        if (root == null) {\n",
        "            return \"\";\n",
        "        }\n",
        "        if (root.left == null && root.right == null) {\n",
        "            return Integer.toString(root.val);\n",
        "        }\n",
        "        if (root.right == null) {\n",
        "            return new StringBuffer().append(root.val).append(\"(\").append(tree2str(root.left)).append(\")\").toString();\n",
        "        }\n",
        "        return new StringBuffer().append(root.val)\n",
        "        .append(\"(\").append(tree2str(root.left)).append(\")(\")\n",
        "        .append(tree2str(root.right)).append(\")\").toString();\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "982126d9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=322 lang=java\n",
        " *\n",
        " * [322] 零钱兑换\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int coinChange(int[] coins, int amount) {\n",
        "        int max = amount +1;\n",
        "        int[] dp = new int[amount+1];\n",
        "        Arrays.fill(dp,max);\n",
        "\n",
        "        dp[0] =0;\n",
        "        for(int i =1;i<=amount;i++){\n",
        "            for(int j =0;j<coins.length;j++){\n",
        "                if(coins[j] <= i){\n",
        "                    dp[i] = Math.min(dp[i],dp[i-coins[j]] +1);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return dp[amount] > amount ? -1:dp[amount];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "65bc4089",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=173 lang=java\n",
        " *\n",
        " * [173] 二叉搜索树迭代器\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class BSTIterator {\n",
        "   public  ListNode ln = new ListNode(-1);\n",
        "   public ListNode head = ln;\n",
        "\n",
        "    public BSTIterator(TreeNode root) {\n",
        "        //使用中序遍历初始化ListNode\n",
        "        traverse(root);\n",
        "    }\n",
        "    \n",
        "    public int next() {\n",
        "        int val = head.next.val;\n",
        "        head = head.next;\n",
        "        return val;\n",
        "    }\n",
        "    \n",
        "    public boolean hasNext() {\n",
        "        return head.next != null;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        // 中序遍历 使用ln,\n",
        "        if(root == null) return ;\n",
        "\n",
        "        traverse(root.left);\n",
        "        ln.next = new ListNode(root.val);\n",
        "        ln = ln.next;\n",
        "        traverse(root.right);\n",
        "\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your BSTIterator object will be instantiated and called as such:\n",
        " * BSTIterator obj = new BSTIterator(root);\n",
        " * int param_1 = obj.next();\n",
        " * boolean param_2 = obj.hasNext();\n",
        " */\n",
        "\n",
        "/**\n",
        " * Your BSTIterator object will be instantiated and called as such:\n",
        " * BSTIterator obj = new BSTIterator(root);\n",
        " * int param_1 = obj.next();\n",
        " * boolean param_2 = obj.hasNext();\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "db6fe0ff",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=863 lang=java\n",
        " *\n",
        " * [863] 二叉树中所有距离为 K 的结点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    Map<Integer, TreeNode> parents = new HashMap<Integer, TreeNode>();\n",
        "    List<Integer> ans = new ArrayList<Integer>();\n",
        "\n",
        "    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {\n",
        "        // 从 root 出发 DFS，记录每个结点的父结点\n",
        "        findParents(root);\n",
        "\n",
        "        // 从 target 出发 DFS，寻找所有深度为 k 的结点\n",
        "        findAns(target, null, 0, k);\n",
        "\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public void findParents(TreeNode node) {\n",
        "        if (node.left != null) {\n",
        "            parents.put(node.left.val, node);\n",
        "            findParents(node.left);\n",
        "        }\n",
        "        if (node.right != null) {\n",
        "            parents.put(node.right.val, node);\n",
        "            findParents(node.right);\n",
        "        }\n",
        "    }\n",
        "\n",
        "    public void findAns(TreeNode node, TreeNode from, int depth, int k) {\n",
        "        //base case\n",
        "        if (node == null) {\n",
        "            return;\n",
        "        }\n",
        "        //base case\n",
        "        if (depth == k) {\n",
        "            ans.add(node.val);\n",
        "            return;\n",
        "        }\n",
        "        if (node.left != from) {\n",
        "            findAns(node.left, node, depth + 1, k);\n",
        "        }\n",
        "        if (node.right != from) {\n",
        "            findAns(node.right, node, depth + 1, k);\n",
        "        }\n",
        "        if (parents.get(node.val) != from) {\n",
        "            findAns(parents.get(node.val), node, depth + 1, k);\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2e6bbc11",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=62 lang=java\n",
        " *\n",
        " * [62] 不同路径\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int uniquePaths(int m, int n) {\n",
        "        int[] cur = new int[n];\n",
        "        Arrays.fill(cur,1);\n",
        "        for(int i=1;i<m;i++){\n",
        "            for(int j=1;j<n;j++){\n",
        "                cur[j] += cur[j-1];\n",
        "            }\n",
        "        }\n",
        "        return cur[n-1];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "540ff33b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1325 lang=java\n",
        " *\n",
        " * [1325] 删除给定值的叶子节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode removeLeafNodes(TreeNode root, int target) {\n",
        "        if(root == null) return null;\n",
        "        TreeNode left = removeLeafNodes(root.left,target);\n",
        "        TreeNode right = removeLeafNodes(root.right,target);\n",
        "        \n",
        "        //后序位置\n",
        "        if(left == null && right == null&& root.val ==target){\n",
        "            return null;\n",
        "        }\n",
        "        // 缺少这个步骤\n",
        "        root.left = left;\n",
        "        root.right = right;\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8ce6cca4",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=653 lang=java\n",
        " *\n",
        " * [653] 两数之和 IV - 输入二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    Set<Integer> res = new HashSet<>();\n",
        "    public boolean findTarget(TreeNode root, int k) {\n",
        "        if(root == null) return false;\n",
        "        \n",
        "        int tmp = k - root.val;\n",
        "\n",
        "        if(res.contains(tmp)){\n",
        "            return true;\n",
        "        }\n",
        "        res.add(root.val);\n",
        "\n",
        "      return  findTarget(root.left,k)||findTarget(root.right,k);\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7df03de5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=315 lang=java\n",
        " *\n",
        " * [315] 计算右侧小于当前元素的个数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    private class Pair{\n",
        "        int val,id;\n",
        "        Pair(int val,int id){\n",
        "            this.id =id;\n",
        "            this.val =val;\n",
        "        }\n",
        "        public String toString(){\n",
        "            return this.id+\"|\"+this.val;\n",
        "        }\n",
        "\n",
        "    }\n",
        "    //归并排序所用的辅助数组\n",
        "    private Pair[] temp;\n",
        "    //记录每个元素后面比自己小的元素个数\n",
        "    private int[] count;\n",
        "\n",
        "    //主函数\n",
        "    public List<Integer> countSmaller(int[] nums) {\n",
        "        int n = nums.length;\n",
        "        count = new int[n];\n",
        "        temp = new Pair[n];\n",
        "        Pair[] arr = new Pair[n];\n",
        "        for(int i=0;i<n;i++){\n",
        "            arr[i] = new Pair(nums[i],i);\n",
        "        }\n",
        "        //执行归并排序\n",
        "        sort(arr,0,n-1);\n",
        "\n",
        "        \n",
        "        List<Integer> res = new LinkedList<>();\n",
        "        for(int c:count){\n",
        "            res.add(c);\n",
        "        }\n",
        "        return res;\n",
        "\n",
        "    }\n",
        "\n",
        "    //归并排序\n",
        "    private void sort(Pair[] arr,int lo,int hi){\n",
        "        if(lo == hi) return ;\n",
        "        int mid = (lo + hi)/2;\n",
        "\n",
        "        sort(arr,lo,mid);\n",
        "        sort(arr,mid+1,hi);\n",
        "\n",
        "        /* 后序位置 */\n",
        "        merge(arr,lo,mid,hi);\n",
        "    }\n",
        "    //合并两个有序数组\n",
        "    private void merge(Pair[] arr,int lo,int mid,int hi){\n",
        "        //区域数组赋值给temp，因为arr后面会存储已排序的数据\n",
        "        for(int i=lo;i<=hi;i++){\n",
        "            temp[i] = arr[i];\n",
        "        }\n",
        "        //i 为左边数组的起始索引\n",
        "        //j 为右边数组的起始索引\n",
        "        int i = lo,j = mid+1;\n",
        "       // System.out.println(\"merge开始->lo:\"+lo+\",hi:\"+hi+\",arr:\"+Arrays.toString(arr));\n",
        "        for(int p = lo;p<=hi;p++){\n",
        "            //在此循环内 i和j 交替增加\n",
        "            //System.out.println(\"p:\"+p+\",i:\"+i+\",j:\"+j);\n",
        "            if(i == mid+1){\n",
        "                arr[p] =temp[j++];\n",
        "            }else if(j == hi+1){\n",
        "                arr[p] = temp[i++];\n",
        "                //更新count数组 采用累加的方式\n",
        "                count[arr[p].id]+= j-(mid+1);\n",
        "            }else if(temp[i].val > temp[j].val){\n",
        "                arr[p] = temp[j++];\n",
        "            }else{\n",
        "                arr[p] = temp[i++];\n",
        "                //更新count 数组\n",
        "                count[arr[p].id]+= j-(mid+1);\n",
        "            \n",
        "            }\n",
        "        }\n",
        "    }\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "613f5624",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=70 lang=java\n",
        " *\n",
        " * [70] 爬楼梯\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int climbStairs(int n) {\n",
        "        int f1 =0,f2 = 0,f3=1;\n",
        "        for(int i = 1; i<=n;++i){\n",
        "            f1 =f2;\n",
        "            f2 =f3;\n",
        "            f3 = f1 +f2;\n",
        "        }\n",
        "        return f3;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7ebba3d1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=965 lang=java\n",
        " *\n",
        " * [965] 单值二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean isUnivalTree(TreeNode root) {\n",
        "        if (root == null) {\n",
        "            return true;\n",
        "        }\n",
        "        if (root.left != null) {\n",
        "            if (root.val != root.left.val || !isUnivalTree(root.left)) {\n",
        "                return false;\n",
        "            }\n",
        "        }\n",
        "        if (root.right != null) {\n",
        "            if (root.val != root.right.val || !isUnivalTree(root.right)) {\n",
        "                return false;\n",
        "            }\n",
        "        }\n",
        "        return true;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0a138dc9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2236 lang=java\n",
        " *\n",
        " * [2236] 判断根结点是否等于子结点之和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean checkTree(TreeNode root) {\n",
        "        if(root.val == root.left.val +root.right.val){\n",
        "            return true;\n",
        "        }\n",
        "        return false;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "abbc063b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=17 lang=java\n",
        " *\n",
        " * [17] 电话号码的字母组合\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<String> letterCombinations(String digits) {\n",
        "        List<String> combinantions = new ArrayList<String>();\n",
        "        if(digits.length() == 0){\n",
        "            return combinantions;\n",
        "        }\n",
        "\n",
        "        Map<Character,String> phoneMap = new HashMap<Character,String>(){{\n",
        "            put('2',\"abc\");\n",
        "            put('3',\"def\");\n",
        "            put('4',\"ghi\");\n",
        "            put('5',\"jkl\");\n",
        "            put('6',\"mno\");\n",
        "            put('7',\"qprs\");\n",
        "            put('8',\"tuv\");\n",
        "            put('9',\"wxyz\");\n",
        "        }};\n",
        "        backtrack(combinantions,phoneMap,digits,0,new StringBuffer());\n",
        "        return combinantions;\n",
        "    }\n",
        "    public void backtrack(List<String> combinantions,Map<Character,String> phoneMap,\n",
        "    String digits,int index,StringBuffer combination){\n",
        "        if(index == digits.length()){\n",
        "            combinantions.add(combination.toString());\n",
        "        }else{\n",
        "            char digit = digits.charAt(index);\n",
        "            String letters = phoneMap.get(digit);\n",
        "            int lettersCount = letters.length();\n",
        "            for(int i=0;i<lettersCount;i++){\n",
        "                combination.append(letters.charAt(i));\n",
        "                backtrack(combinantions,phoneMap,digits,index+1,combination);\n",
        "                combination.deleteCharAt(index);\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "241b18e4",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=83 lang=java\n",
        " *\n",
        " * [83] 删除排序链表中的重复元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode deleteDuplicates(ListNode head) {\n",
        "        if(head == null) return null;\n",
        "        ListNode slow =head,fast =head;\n",
        "        while(fast != null){\n",
        "            if(fast.val !=slow.val){\n",
        "                slow.next = fast;\n",
        "                slow = slow.next;\n",
        "            }\n",
        "            fast =fast.next;\n",
        "        }\n",
        "        slow.next = null;\n",
        "        return head;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "fbd593cb",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=344 lang=java\n",
        " *\n",
        " * [344] 反转字符串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public void reverseString(char[] s) {\n",
        "        if(s.length <2) return;\n",
        "        int left = 0,right =s.length-1;\n",
        "        while(left < right){\n",
        "            char temp = s[left];\n",
        "            s[left] = s[right];\n",
        "            s[right] =temp;\n",
        "            left ++;\n",
        "            right--;\n",
        "        }\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "de1dd8c5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=300 lang=java\n",
        " *\n",
        " * [300] 最长递增子序列\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int lengthOfLIS(int[] nums) {\n",
        "      if(nums.length == 0){\n",
        "          return 0;\n",
        "      }\n",
        "      int[] dp = new int[nums.length];\n",
        "      dp[0] = 1;\n",
        "      int maxans = 1;\n",
        "      for(int i=0;i<nums.length;i++){\n",
        "          dp[i] =1;\n",
        "          for(int j=0;j<i;j++){\n",
        "              if(nums[i] > nums[j]){\n",
        "                  dp[i] = Math.max(dp[i],dp[j]+1);\n",
        "              }\n",
        "          }\n",
        "          maxans =Math.max(maxans,dp[i]);\n",
        "      }\n",
        "        \n",
        "      return maxans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e5131b06",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=235 lang=java\n",
        " *\n",
        " * [235] 二叉搜索树的最近公共祖先\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "      public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {\n",
        "        return find(root,p.val,q.val);\n",
        "    }\n",
        "\n",
        "    TreeNode find(TreeNode root,int val1,int val2){\n",
        "        if(root == null){\n",
        "            return null;\n",
        "        }\n",
        "        //前序位置\n",
        "        if(root.val == val1 || root.val == val2){\n",
        "            //如果遇到目标值，直接返回\n",
        "            return root;\n",
        "\n",
        "        }\n",
        "        TreeNode left = find(root.left,val1,val2);\n",
        "        TreeNode right = find(root.right,val1,val2);\n",
        "\n",
        "        //后序位置\n",
        "        if(left!= null && right !=null){\n",
        "            //当前节点是LCA节点\n",
        "            return root;\n",
        "        }\n",
        "        // 因为题目说了p和q一定存在于二叉树中(这点很重要），\n",
        "        //所以即便我们遇到q就直接返回，根本没遍历到p，也依然可以断定p在q底下，q就是LCA节点。\n",
        "        return left != null ? left:right;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cea69a91",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1022 lang=java\n",
        " *\n",
        " * [1022] 从根到叶的二进制数之和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public int sumRootToLeaf(TreeNode root) {\n",
        "        return dfs(root, 0);\n",
        "    }\n",
        "\n",
        "    public int dfs(TreeNode root, int val) {\n",
        "        if (root == null) {\n",
        "            return 0;\n",
        "        }\n",
        "        val = (val << 1) | root.val;\n",
        "        if (root.left == null && root.right == null) {\n",
        "            return val;\n",
        "        }\n",
        "        return dfs(root.left, val) + dfs(root.right, val);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9bdac99b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=938 lang=java\n",
        " *\n",
        " * [938] 二叉搜索树的范围和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int sum =0;\n",
        "    public int rangeSumBST(TreeNode root, int low, int high) {\n",
        "\n",
        "        traverse(root,low,high);\n",
        "        return sum;\n",
        "\n",
        "    }\n",
        "    void traverse(TreeNode root, int low, int high){\n",
        "        if(root == null) return ;\n",
        "        \n",
        "\n",
        "        traverse(root.left,low,high);\n",
        "        if(root.val >=low && root.val <=high){\n",
        "            sum+=root.val;\n",
        "        }\n",
        "        traverse(root.right,low,high);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "66327a11",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=142 lang=java\n",
        " *\n",
        " * [142] 环形链表 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode(int x) {\n",
        " *         val = x;\n",
        " *         next = null;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "public class Solution {\n",
        "    public ListNode detectCycle(ListNode head) {\n",
        "        //方法体start\n",
        "        ListNode slow =head,fast = head;\n",
        "        while(fast!=null && fast.next!=null){\n",
        "            slow = slow.next;\n",
        "            fast =fast.next.next;\n",
        "            if(fast == slow) break;\n",
        "        }\n",
        "        // 无环\n",
        "        if(fast == null || fast.next == null){\n",
        "            return null;\n",
        "        }\n",
        "        //slow 从头节点出发，fast 从相遇节点出发\n",
        "        slow = head;\n",
        "        while(slow != fast){\n",
        "            slow = slow.next;\n",
        "            fast = fast.next;\n",
        "        }\n",
        "        return slow;\n",
        "        \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6184e003",
      "metadata": {},
      "outputs": [],
      "source": [
        "public class test {\n",
        "    public static void main(String[] args) {\n",
        "        System.out.println(\"Hello World\");\n",
        "        System.out.println('c');\n",
        "        \n",
        "\n",
        "    }\n",
        "}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a76088c1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=7 lang=java\n",
        " *\n",
        " * [7] 整数反转\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int reverse(int x) {\n",
        "        int res = 0;\n",
        "        int max = Integer.MAX_VALUE /10;\n",
        "        int max_tmp = Integer.MAX_VALUE %10;\n",
        "        int min = Integer.MIN_VALUE /10;\n",
        "        int min_tmp = Integer.MIN_VALUE %10;\n",
        "        while(x!=0){\n",
        "            // 每次取末尾数字\n",
        "            int tmp = x%10;\n",
        "\n",
        "            // 判断是否大于最大32位整数\n",
        "            if(res > max || (res ==max && max_tmp >7)){\n",
        "                return 0;\n",
        "            } \n",
        "            // 判断是否小于最小32位整数\n",
        "            if(res < min ||(res == min && min_tmp < -8)){\n",
        "                return 0;\n",
        "            }\n",
        "\n",
        "            res = res*10 +tmp;\n",
        "            x /=10;\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8a4931c6",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=160 lang=java\n",
        " *\n",
        " * [160] 相交链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode(int x) {\n",
        " *         val = x;\n",
        " *         next = null;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "public class Solution {\n",
        "    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {\n",
        "        ListNode p1 = headA,p2 = headB;\n",
        "        while(p1!=p2){\n",
        "            if(p1 !=null){\n",
        "                p1 = p1.next;\n",
        "            }else{\n",
        "                p1 =headB;\n",
        "            }\n",
        "\n",
        "             if(p2 !=null){\n",
        "                p2 = p2.next;\n",
        "            }else{\n",
        "                p2 =headA;\n",
        "            }\n",
        "\n",
        "        }\n",
        "        return p1;\n",
        "        \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4aaa5764",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=572 lang=java\n",
        " *\n",
        " * [572] 另一棵树的子树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean isSubtree(TreeNode s, TreeNode t) {\n",
        "        if (t == null) return true;   // t 为 null 一定都是 true\n",
        "        if (s == null) return false;  // 这里 t 一定不为 null, 只要 s 为 null，肯定是 false\n",
        "        return isSubtree(s.left, t) || isSubtree(s.right, t) || isSameTree(s,t);\n",
        "    }\n",
        "\n",
        "    /**\n",
        "     * 判断两棵树是否相同\n",
        "     */\n",
        "    public boolean isSameTree(TreeNode s, TreeNode t){\n",
        "        if (s == null && t == null) return true;\n",
        "        if (s == null || t == null) return false;\n",
        "        if (s.val != t.val) return false;\n",
        "        return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b83f0011",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=438 lang=java\n",
        " *\n",
        " * [438] 找到字符串中所有字母异位词\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "\n",
        "public List<Integer> findAnagrams(String s, String t) {\n",
        "    Map<Character, Integer> need = new HashMap<>();\n",
        "    Map<Character, Integer> window = new HashMap<>();\n",
        "    for (int i = 0; i < t.length(); i++) {\n",
        "        char c = t.charAt(i);\n",
        "        need.put(c, need.getOrDefault(c, 0) + 1);\n",
        "    }\n",
        "\n",
        "    int left = 0, right = 0;\n",
        "    int valid = 0;\n",
        "    List<Integer> res = new ArrayList<>(); // 记录结果\n",
        "    while (right < s.length()) {\n",
        "        char c = s.charAt(right);\n",
        "        right++;\n",
        "        // 进行窗口内数据的一系列更新\n",
        "        if (need.containsKey(c)) {\n",
        "            window.put(c, window.getOrDefault(c, 0) + 1);\n",
        "            if (window.get(c).equals(need.get(c))) {\n",
        "                valid++;\n",
        "            }\n",
        "        }\n",
        "        // 判断左侧窗口是否要收缩\n",
        "        while (right - left >= t.length()) {\n",
        "            // 当窗口符合条件时，把起始索引加入 res\n",
        "            if (valid == need.size()) {\n",
        "                res.add(left);\n",
        "            }\n",
        "            char d = s.charAt(left);\n",
        "            left++;\n",
        "            // 进行窗口内数据的一系列更新\n",
        "            if (need.containsKey(d)) {\n",
        "                if (window.get(d).equals(need.get(d))) {\n",
        "                    valid--;\n",
        "                }\n",
        "                window.put(d, window.get(d) - 1);\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "    return res;\n",
        "}\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "03cd207d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=24 lang=java\n",
        " *\n",
        " * [24] 两两交换链表中的节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode swapPairs(ListNode head) {\n",
        "        if (head == null || head.next == null) {\n",
        "            return head;\n",
        "        }\n",
        "        ListNode newHead = head.next;\n",
        "        head.next = swapPairs(newHead.next);\n",
        "        newHead.next = head;\n",
        "        return newHead;\n",
        "    }\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f30cd14f",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=797 lang=java\n",
        " *\n",
        " * [797] 所有可能的路径\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    //记录所有路径\n",
        "    List<List<Integer>> res  =new LinkedList<>();\n",
        "    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {\n",
        "        //维护递归过程中经过的路径\n",
        "        LinkedList<Integer> path = new LinkedList<>();\n",
        "        traverse(graph,0,path);\n",
        "        return res;\n",
        "    }\n",
        "    void traverse(int[][] graph,int s, LinkedList<Integer> path){\n",
        "        //添加节点s 到路径\n",
        "        path.addLast(s);\n",
        "        int n = graph.length;\n",
        "        if(s == n-1){\n",
        "            //到达终点\n",
        "            //注意 Java 的语言特性，向res中添加path时需要拷贝一个新的列表，否则最终res中的列表都是空的。\n",
        "            res.add(new LinkedList<>(path));\n",
        "            path.removeLast();\n",
        "            return;\n",
        "        }\n",
        "        //递归每个相邻节点\n",
        "        for(int v : graph[s]){\n",
        "            traverse(graph,v,path);\n",
        "        }\n",
        "        //从路径移出节点s\n",
        "        path.removeLast();\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e07386f1",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=703 lang=java\n",
        " *\n",
        " * [703] 数据流中的第 K 大元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class KthLargest {\n",
        "    PriorityQueue<Integer> pq;\n",
        "    int k;\n",
        "\n",
        "    public KthLargest(int k, int[] nums) {\n",
        "        this.k = k;\n",
        "        pq = new PriorityQueue<Integer>();\n",
        "        for (int x : nums) {\n",
        "            add(x);\n",
        "        }\n",
        "    }\n",
        "    \n",
        "    public int add(int val) {\n",
        "        pq.offer(val);\n",
        "        if (pq.size() > k) {\n",
        "            pq.poll();\n",
        "        }\n",
        "        return pq.peek();\n",
        "    }\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your KthLargest object will be instantiated and called as such:\n",
        " * KthLargest obj = new KthLargest(k, nums);\n",
        " * int param_1 = obj.add(val);\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4fff6335",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=841 lang=java\n",
        " *\n",
        " * [841] 钥匙和房间\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean canVisitAllRooms(List<List<Integer>> rooms) {\n",
        "        int  n = rooms.size();\n",
        "        Set<Integer> roomIndexs = new HashSet<>();\n",
        "        for(int i=1;i<n;i++){\n",
        "            roomIndexs.add(i);\n",
        "        }\n",
        "        enter(rooms.get(0),roomIndexs,rooms);\n",
        "\n",
        "        return roomIndexs.size()==0;\n",
        "    }\n",
        "\n",
        "    public void enter( List<Integer> item, Set<Integer> roomIndexs,List<List<Integer>> rooms){\n",
        "        for(int i=0;i<item.size();i++){\n",
        "            if(roomIndexs.contains(item.get(i))){\n",
        "                \n",
        "                roomIndexs.remove(item.get(i));\n",
        "                enter(rooms.get(item.get(i)),roomIndexs,rooms);\n",
        "            }\n",
        "            \n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "fdfb756a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1431 lang=java\n",
        " *\n",
        " * [1431] 拥有最多糖果的孩子\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {\n",
        "        int n = candies.length;\n",
        "        int maxCandies = 0;\n",
        "        //获取最多的糖果数量\n",
        "        for(int i =0;i<n;++i){\n",
        "            maxCandies = Math.max(maxCandies,candies[i]);\n",
        "        }\n",
        "        List<Boolean> ret = new ArrayList<Boolean>();\n",
        "        for(int i=0;i<n;++i){\n",
        "            ret.add(candies[i]+extraCandies >=maxCandies);\n",
        "        }\n",
        "        return ret;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d6c4d318",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=735 lang=java\n",
        " *\n",
        " * [735] 小行星碰撞\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] asteroidCollision(int[] asteroids) {\n",
        "        Deque<Integer> stack = new ArrayDeque<Integer>();\n",
        "        for(int aster : asteroids){\n",
        "            boolean alive =true;\n",
        "            while(alive && aster < 0 && !stack.isEmpty() && stack.peek()>0 ){\n",
        "                alive = stack.peek() < -aster ;\n",
        "                if(stack.peek() <= -aster){\n",
        "                    //栈顶行星爆炸\n",
        "                    stack.pop();\n",
        "                }\n",
        "            }\n",
        "            if(alive){\n",
        "                stack.push(aster);\n",
        "            }\n",
        "        }\n",
        "        int size = stack.size();\n",
        "        int[] ans = new int[size];\n",
        "        for(int i=size-1;i>=0;i--){\n",
        "            ans[i] = stack.pop();\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6740f734",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=14 lang=java\n",
        " * @lcpr version=20003\n",
        " *\n",
        " * [14] 最长公共前缀\n",
        " */\n",
        "\n",
        "\n",
        "// @lcpr-template-start\n",
        "\n",
        "// @lcpr-template-end\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String longestCommonPrefix(String[] strs) {\n",
        "        int m = strs.length;\n",
        "        //以第一行的列数为基准\n",
        "        int n = strs[0].length();\n",
        "        for(int col =0;col<n;col++){\n",
        "            for(int row =1;row<m;row++){\n",
        "                String thisStr =strs[row],preStr  =strs[row-1];\n",
        "                //判断每个字符串的col索引是否都相同\n",
        "                if(col >= thisStr.length() || col >= preStr.length() || thisStr.charAt(col)!= preStr.charAt(col)){\n",
        "                    // 发现不匹配的字符，只有strs[row][0..col-1]是公共前缀\n",
        "                    return strs[0].substring(0,col);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return strs[0];\n",
        "        \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n",
        "\n",
        "\n",
        "/*\n",
        "// @lcpr case=start\n",
        "// [\"flower\",\"flow\",\"flight\"]\\n\n",
        "// @lcpr case=end\n",
        "\n",
        "// @lcpr case=start\n",
        "// [\"dog\",\"racecar\",\"car\"]\\n\n",
        "// @lcpr case=end\n",
        "\n",
        " */\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0b889cce",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1456 lang=java\n",
        " *\n",
        " * [1456] 定长子串中元音的最大数目\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int maxVowels(String s, int k) {\n",
        "        int n = s.length();\n",
        "        int vowel_count = 0;\n",
        "        for (int i = 0; i < k; ++i) {\n",
        "            vowel_count += isVowel(s.charAt(i));\n",
        "        }\n",
        "        int ans = vowel_count;\n",
        "        for (int i = k; i < n; ++i) {\n",
        "            vowel_count += isVowel(s.charAt(i)) - isVowel(s.charAt(i - k));\n",
        "            ans = Math.max(ans, vowel_count);\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public int isVowel(char ch) {\n",
        "        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ? 1 : 0;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "70cc14e4",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1732 lang=java\n",
        " *\n",
        " * [1732] 找到最高海拔\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int largestAltitude(int[] gain) {\n",
        "        int ans = 0, sum =0;\n",
        "        for(int x : gain){\n",
        "            sum +=x;\n",
        "            ans = Math.max(sum,ans);\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4e2ed20d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=148 lang=java\n",
        " *\n",
        " * [148] 排序链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " * 归并排序\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode sortList(ListNode head) {\n",
        "        if(head == null || head.next == null){\n",
        "            return head;\n",
        "        }\n",
        "        ListNode fast = head.next,slow = head;\n",
        "        while(fast!=null && fast.next!=null){\n",
        "            slow =slow.next;\n",
        "            fast = fast.next.next;\n",
        "        }\n",
        "        ListNode tmp =slow.next;\n",
        "        slow.next = null;\n",
        "        ListNode left = sortList(head);\n",
        "        ListNode right = sortList(tmp);\n",
        "        ListNode h = new ListNode(0);\n",
        "        ListNode res = h;\n",
        "        while(left !=null && right !=null){\n",
        "            if(left.val < right.val){\n",
        "                h.next = left;\n",
        "                left = left.next;\n",
        "            }else{\n",
        "                h.next = right;\n",
        "                right = right.next;\n",
        "            }\n",
        "            h = h.next;\n",
        "        }\n",
        "        h.next = left!=null ? left :right;\n",
        "        return res.next;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b6225e35",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2196 lang=java\n",
        " *\n",
        " * [2196] 根据描述创建二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode createBinaryTree(int[][] descriptions) {\n",
        "        // 存储所有的子节点\n",
        "        Set<Integer> chlids = new HashSet<>();\n",
        "        \n",
        "        Map<Integer, TreeNode> map = new HashMap<>();\n",
        "        // 构造树\n",
        "        for (int[] d : descriptions) {\n",
        "            int pVal = d[0], cVal = d[1];\n",
        "            boolean left = (d[2] == 1) ? true : false;\n",
        "\n",
        "            TreeNode pNode = map.get(pVal);\n",
        "            if (pNode == null) {\n",
        "                pNode = new TreeNode(pVal);\n",
        "                map.put(pVal, pNode);\n",
        "            }\n",
        "\n",
        "            TreeNode cNode = map.get(cVal);\n",
        "            if (cNode == null) {\n",
        "                cNode = new TreeNode(cVal);\n",
        "                map.put(cVal, cNode);\n",
        "            }\n",
        "            chlids.add(cVal);\n",
        "\n",
        "            if (left) {\n",
        "                pNode.left = cNode;\n",
        "            } else {\n",
        "                pNode.right = cNode;\n",
        "            }\n",
        "        }\n",
        "\n",
        "        // 找到根节点\n",
        "        TreeNode root = null;\n",
        "        for (int v : map.keySet()) {\n",
        "            if (chlids.contains(v)) continue;\n",
        "            else {\n",
        "                root = map.get(v);\n",
        "                break;\n",
        "            }\n",
        "        }\n",
        "\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9e809d3e",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=31 lang=java\n",
        " *\n",
        " * [31] 下一个排列\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public void nextPermutation(int[] nums) {\n",
        "        int i = nums.length - 2;\n",
        "        while(i >= 0 && nums[i] >= nums[i + 1]){\n",
        "            i-- ;\n",
        "        }\n",
        "        if(i >= 0){\n",
        "            int j = nums.length - 1;\n",
        "            while(j >= 0 && nums[i] >= nums[j]){\n",
        "                j--;\n",
        "            }\n",
        "            swap(nums,i,j);\n",
        "        }\n",
        "        reverse(nums,i+1);\n",
        "    }\n",
        "    public void swap(int[] nums,int i,int j){\n",
        "        int temp = nums[i];\n",
        "        nums[i] = nums[j];\n",
        "        nums[j] = temp;\n",
        "    }\n",
        "\n",
        "    public void reverse(int[] nums,int start){\n",
        "        int left = start,right = nums.length -1;\n",
        "        while(left < right){\n",
        "            swap(nums,left,right);\n",
        "            left++;\n",
        "            right--;\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9d682940",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=116 lang=java\n",
        " *\n",
        " * [116] 填充每个节点的下一个右侧节点指针\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/*\n",
        "// Definition for a Node.\n",
        "class Node {\n",
        "    public int val;\n",
        "    public Node left;\n",
        "    public Node right;\n",
        "    public Node next;\n",
        "\n",
        "    public Node() {}\n",
        "    \n",
        "    public Node(int _val) {\n",
        "        val = _val;\n",
        "    }\n",
        "\n",
        "    public Node(int _val, Node _left, Node _right, Node _next) {\n",
        "        val = _val;\n",
        "        left = _left;\n",
        "        right = _right;\n",
        "        next = _next;\n",
        "    }\n",
        "};\n",
        "*/\n",
        "class Solution {\n",
        "    public Node connect(Node root) {\n",
        "        if(root == null) return null;\n",
        "        traverse(root.left,root.right);\n",
        "        return root;\n",
        "    }\n",
        "    void traverse(Node node1,Node node2){\n",
        "        if(node1 == null || node2 == null){\n",
        "            return ;\n",
        "        }\n",
        "        // 将传入的两个节点串起来\n",
        "        node1.next = node2;\n",
        "        //连接相同父节点的两个子节点\n",
        "        traverse(node1.left,node1.right);\n",
        "        traverse(node2.left,node2.right);\n",
        "\n",
        "         //连接跨越父节点的两个子节点\n",
        "         traverse(node1.right,node2.left);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5004c78e",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=814 lang=java\n",
        " *\n",
        " * [814] 二叉树剪枝\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode pruneTree(TreeNode root) {\n",
        "        if (root == null) {\n",
        "            return null;\n",
        "        }\n",
        "        root.left = pruneTree(root.left);\n",
        "        root.right = pruneTree(root.right);\n",
        "        if (root.left == null && root.right == null && root.val == 0) {\n",
        "            return null;\n",
        "        }\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9a38f757",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=4 lang=java\n",
        " *\n",
        " * [4] 寻找两个正序数组的中位数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public double findMedianSortedArrays(int[] nums1, int[] nums2) {\n",
        "        \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e2f5b794",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=933 lang=java\n",
        " *\n",
        " * [933] 最近的请求次数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class RecentCounter {\n",
        "    List<Integer> requests;\n",
        "\n",
        "    public RecentCounter() {\n",
        "        requests = new ArrayList<>();\n",
        "    }\n",
        "    \n",
        "    public int ping(int t) {\n",
        "        requests.add(t);\n",
        "        int pre = t-3000;\n",
        "        int cnt = 0;\n",
        "        for(int i=requests.size() -1;i>=0;i--){\n",
        "            if(requests.get(i)>= (t-3000)){\n",
        "                cnt++;\n",
        "            }else{\n",
        "                break;\n",
        "            }\n",
        "        }\n",
        "        return cnt;\n",
        "    }\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your RecentCounter object will be instantiated and called as such:\n",
        " * RecentCounter obj = new RecentCounter();\n",
        " * int param_1 = obj.ping(t);\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a7caad31",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=145 lang=java\n",
        " *\n",
        " * [145] 二叉树的后序遍历\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "    List<Integer> res = new LinkedList<>();\n",
        "    public List<Integer> postorderTraversal(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "\n",
        "        traverse(root.left);\n",
        "        traverse(root.right);\n",
        "        res.add(root.val);\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1900671b",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=200 lang=java\n",
        " *\n",
        " * [200] 岛屿数量\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    // 主函数，计算岛屿数量\n",
        "    public int numIslands(char[][] grid) {\n",
        "        int res = 0;\n",
        "        int m = grid.length, n = grid[0].length;\n",
        "        // 遍历 grid\n",
        "        for (int i = 0; i < m; i++) {\n",
        "            for (int j = 0; j < n; j++) {\n",
        "                if (grid[i][j] == '1') {\n",
        "                    // 每发现一个岛屿，岛屿数量加一\n",
        "                    res++;\n",
        "                    // 然后使用 DFS 将岛屿淹了\n",
        "                    dfs(grid, i, j);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    // 从 (i, j) 开始，将与之相邻的陆地都变成海水\n",
        "    void dfs(char[][] grid, int i, int j) {\n",
        "        int m = grid.length, n = grid[0].length;\n",
        "        if (i < 0 || j < 0 || i >= m || j >= n) {\n",
        "            // 超出索引边界\n",
        "            return;\n",
        "        }\n",
        "        if (grid[i][j] == '0') {\n",
        "            // 已经是海水了\n",
        "            return;\n",
        "        }\n",
        "        // 将 (i, j) 变成海水\n",
        "        grid[i][j] = '0';\n",
        "        // 淹没上下左右的陆地\n",
        "        dfs(grid, i + 1, j);\n",
        "        dfs(grid, i, j + 1);\n",
        "        dfs(grid, i - 1, j);\n",
        "        dfs(grid, i, j - 1);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "dc8edb9d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=563 lang=java\n",
        " *\n",
        " * [563] 二叉树的坡度\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int ans = 0;\n",
        "\n",
        "    public int findTilt(TreeNode root) {\n",
        "        dfs(root);\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public int dfs(TreeNode node) {\n",
        "        if (node == null) {\n",
        "            return 0;\n",
        "        }\n",
        "        int sumLeft = dfs(node.left);\n",
        "        int sumRight = dfs(node.right);\n",
        "        ans += Math.abs(sumLeft - sumRight);\n",
        "        return sumLeft + sumRight + node.val;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ca471ca5",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=704 lang=java\n",
        " *\n",
        " * [704] 二分查找\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int search(int[] nums, int target) {\n",
        "        int left = 0, right = nums.length - 1;\n",
        "        while (left <= right) {\n",
        "            int mid = (right + left) >>> 1;\n",
        "            int num = nums[mid];\n",
        "            if (num == target) {\n",
        "                return mid;\n",
        "            } else if (num > target) {\n",
        "                right = mid - 1;\n",
        "            } else {\n",
        "                left = mid + 1;\n",
        "            }\n",
        "        }\n",
        "        return -1;\n",
        "    }\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "9da57baf",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=236 lang=java\n",
        " *\n",
        " * [236] 二叉树的最近公共祖先\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {\n",
        "        return find(root,p.val,q.val);\n",
        "    }\n",
        "\n",
        "    TreeNode find(TreeNode root,int val1,int val2){\n",
        "        if(root == null){\n",
        "            return null;\n",
        "        }\n",
        "        //前序位置\n",
        "        if(root.val == val1 || root.val == val2){\n",
        "            //如果遇到目标值，直接返回\n",
        "            return root;\n",
        "\n",
        "        }\n",
        "        TreeNode left = find(root.left,val1,val2);\n",
        "        TreeNode right = find(root.right,val1,val2);\n",
        "\n",
        "        //后序位置\n",
        "        if(left!= null && right !=null){\n",
        "            //当前节点是LCA节点\n",
        "            return root;\n",
        "        }\n",
        "        // 因为题目说了p和q一定存在于二叉树中(这点很重要），\n",
        "        //所以即便我们遇到q就直接返回，根本没遍历到p，也依然可以断定p在q底下，q就是LCA节点。\n",
        "        return left != null ? left:right;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "59cb1452",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=141 lang=java\n",
        " *\n",
        " * [141] 环形链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode(int x) {\n",
        " *         val = x;\n",
        " *         next = null;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "public class Solution {\n",
        "    public boolean hasCycle(ListNode head) {\n",
        "     ListNode slow = head,fast =head;\n",
        "     while(fast!=null&&fast.next!=null){\n",
        "         slow =slow.next;\n",
        "         fast = fast.next.next;\n",
        "         if(slow == fast){\n",
        "             return true;\n",
        "         }\n",
        "     }\n",
        "     return false;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f15d7712",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=121 lang=java\n",
        " *\n",
        " * [121] 买卖股票的最佳时机\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int maxProfit(int[] prices) {\n",
        "        int minprice = Integer.MAX_VALUE;\n",
        "        int maxprofit =0;\n",
        "        for(int i=0;i<prices.length;i++){\n",
        "            if(prices[i] < minprice){\n",
        "                minprice = prices[i];\n",
        "            }else if(prices[i] - minprice > maxprofit){\n",
        "                maxprofit = prices[i] - minprice;\n",
        "            }\n",
        "        }\n",
        "        return maxprofit;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "017b02c7",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=102 lang=java\n",
        " *\n",
        " * [102] 二叉树的层序遍历\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "    public List<List<Integer>> levelOrder(TreeNode root) {\n",
        "        List<List<Integer>> res = new ArrayList<>();\n",
        "        traverse(root,res);\n",
        "        return res;\n",
        "\n",
        "    }\n",
        "\n",
        "\n",
        "    void traverse(TreeNode root,List<List<Integer>> res){\n",
        "        if (root == null) return;\n",
        "        // 初始化队列，将 root 加入队列\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "\n",
        "        while (!q.isEmpty()) {\n",
        "        List<Integer> levelList = new ArrayList<>();\n",
        "        int sz = q.size();\n",
        "        for(int i=0;i<sz;i++){\n",
        "            TreeNode cur = q.poll();\n",
        "            levelList.add(cur.val);\n",
        "\n",
        "            if (cur.left != null) {\n",
        "                 q.offer(cur.left);\n",
        "            }\n",
        "            if (cur.right != null) {\n",
        "                q.offer(cur.right);\n",
        "            }\n",
        "        }\n",
        "        res.add(levelList);\n",
        "    }\n",
        "\n",
        "\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b32e4c7e",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=152 lang=java\n",
        " *\n",
        " * [152] 乘积最大子数组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int maxProduct(int[] nums) {\n",
        "        int max = Integer.MIN_VALUE,imax =1,imin =1;\n",
        "        for(int i=0;i<nums.length;i++){\n",
        "            if(nums[i] < 0){\n",
        "                int tmp = imax;\n",
        "                imax = imin;\n",
        "                imin = tmp;\n",
        "            }\n",
        "            imax = Math.max(imax*nums[i],nums[i]);\n",
        "            imin = Math.min(imin*nums[i],nums[i]);\n",
        "            System.out.println(\"imax:\"+imax);\n",
        "            System.out.println(\"imin:\"+imin);\n",
        "            max = Math.max(max,imax);\n",
        "        }\n",
        "        return max;\n",
        "    }\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6f8be1e9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=169 lang=java\n",
        " *\n",
        " * [169] 多数元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int majorityElement(int[] nums) {\n",
        "        Arrays.sort(nums);\n",
        "        return nums[nums.length/2];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ad29e258",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1207 lang=java\n",
        " *\n",
        " * [1207] 独一无二的出现次数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean uniqueOccurrences(int[] arr) {\n",
        "        Map<Integer,Integer> appearTimes = new HashMap<>();\n",
        "        for(int x:arr){\n",
        "            appearTimes.put(x,appearTimes.getOrDefault(x,0)+1);\n",
        "        }\n",
        "        Set<Integer> ans = new HashSet<>();\n",
        "        ans.addAll(appearTimes.values());\n",
        "       return  appearTimes.size() == ans.size();\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8de04633",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=501 lang=java\n",
        " *\n",
        " * [501] 二叉搜索树中的众数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    ArrayList<Integer> resList = new ArrayList<>();\n",
        "    int count = 0;\n",
        "    int maxCount = Integer.MIN_VALUE;\n",
        "    TreeNode prev = null;\n",
        "    public int[] findMode(TreeNode root) {\n",
        "        dfs(root);\n",
        "        // 转换为数组输出\n",
        "        int[] res = new int[resList.size()];\n",
        "        for (int i = 0; i < resList.size(); i++) {\n",
        "            res[i] = resList.get(i);\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "    void dfs(TreeNode root){\n",
        "        if(root == null) return;\n",
        "        dfs(root.left);\n",
        "        /** 中序位置 */\n",
        "        if(prev != null && root.val == prev.val){\n",
        "            count++;\n",
        "        }else{\n",
        "            count = 1;\n",
        "        }\n",
        "        if(count == maxCount) resList.add(root.val);\n",
        "        if(count > maxCount){\n",
        "            maxCount = count;\n",
        "            resList.clear();\n",
        "            resList.add(root.val);\n",
        "        }\n",
        "        prev = root;\n",
        "        dfs(root.right);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "187c2204",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1448 lang=java\n",
        " *\n",
        " * [1448] 统计二叉树中好节点的数目\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public int goodNodes(TreeNode root) {\n",
        "        if (root == null) return 0;\n",
        "        dfs(root, root.val);\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    int ans = 0;\n",
        "    void dfs(TreeNode node, int curMax) {\n",
        "        if (node == null) return;\n",
        "        if (node.val >= curMax) {\n",
        "            ans++;\n",
        "            curMax = node.val;\n",
        "        }\n",
        "        dfs(node.left, curMax);\n",
        "        dfs(node.right, curMax);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c0eea692",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=912 lang=java\n",
        " *\n",
        " * [912] 排序数组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] sortArray(int[] nums) {\n",
        "        Merge.sort(nums);\n",
        "        return nums;\n",
        "    }\n",
        "    \n",
        "}\n",
        "class Merge {\n",
        "\n",
        "    // 用于辅助合并有序数组\n",
        "    private static int[] temp;\n",
        "\n",
        "    public static void sort(int[] nums) {\n",
        "        // 先给辅助数组开辟内存空间\n",
        "        temp = new int[nums.length];\n",
        "        // 排序整个数组（原地修改）\n",
        "        sort(nums, 0, nums.length - 1);\n",
        "    }\n",
        "\n",
        "    // 定义：将子数组 nums[lo..hi] 进行排序\n",
        "    private static void sort(int[] nums, int lo, int hi) {\n",
        "        if (lo == hi) {\n",
        "            // 单个元素不用排序\n",
        "            return;\n",
        "        }\n",
        "        // 这样写是为了防止溢出，效果等同于 (hi + lo) / 2\n",
        "        int mid = lo + (hi - lo) / 2;\n",
        "        // 先对左半部分数组 nums[lo..mid] 排序\n",
        "        sort(nums, lo, mid);\n",
        "        // 再对右半部分数组 nums[mid+1..hi] 排序\n",
        "        sort(nums, mid + 1, hi);\n",
        "        // 将两部分有序数组合并成一个有序数组\n",
        "        merge(nums, lo, mid, hi);\n",
        "    }\n",
        "\n",
        "    // 将 nums[lo..mid] 和 nums[mid+1..hi] 这两个有序数组合并成一个有序数组\n",
        "    private static void merge(int[] nums, int lo, int mid, int hi) {\n",
        "        // 先把 nums[lo..hi] 复制到辅助数组中\n",
        "        // 以便合并后的结果能够直接存入 nums\n",
        "        for (int i = lo; i <= hi; i++) {\n",
        "            temp[i] = nums[i];\n",
        "        }\n",
        "\n",
        "        // 数组双指针技巧，合并两个有序数组\n",
        "        int i = lo, j = mid + 1;\n",
        "        for (int p = lo; p <= hi; p++) {\n",
        "            if (i == mid + 1) {\n",
        "                // 左半边数组已全部被合并\n",
        "                nums[p] = temp[j++];\n",
        "            } else if (j == hi + 1) {\n",
        "                // 右半边数组已全部被合并\n",
        "                nums[p] = temp[i++];\n",
        "            } else if (temp[i] > temp[j]) {\n",
        "                nums[p] = temp[j++];\n",
        "            } else {\n",
        "                nums[p] = temp[i++];\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "19298ee6",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=852 lang=java\n",
        " *\n",
        " * [852] 山脉数组的峰顶索引\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int peakIndexInMountainArray(int[] arr) {\n",
        "        int left =0,right =arr.length -1,ans =0;\n",
        "        while(left<=right){\n",
        "            int mid = left +(right-left)/2;\n",
        "            if(arr[mid]>arr[mid-1] &&arr[mid]>arr[mid+1]){\n",
        "                ans = mid;\n",
        "                break;\n",
        "            }\n",
        "            if(arr[mid]< arr[mid+1]){\n",
        "                left = mid;\n",
        "            }else{\n",
        "                right =mid;\n",
        "            }\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "118f7e5f",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1372 lang=java\n",
        " *\n",
        " * [1372] 二叉树中的最长交错路径\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        " /**\n",
        " 采用深度优先遍历的方式，我们可以从顶向下访问到所有节点。并且遍历下一个子节点时，我们也能够知道子节点是属于父节点的左子树，还是右子树。\n",
        "\n",
        "所以我们可以为每个节点缓存两个值，一个l表示到达当前节点时，该节点为左子树时的路径数，一个r表示该节点为右子树时的到达路径。\n",
        "当然，一个节点要么是左子树，要么是右子树，所以l和r其中只有一个有值。\n",
        "\n",
        "那么在遍历该节点的子节点时，如果子节点是左子树，那么它的l值就是父节点的r值加1. 如果是右子树，就是父节点的l值加1.\n",
        "\n",
        " \n",
        "  */\n",
        "class Solution {\n",
        "    private int max = 0;\n",
        "\n",
        "    public int longestZigZag(TreeNode root) {\n",
        "        visit(root, 0, 0);\n",
        "        return max;\n",
        "    }\n",
        "\n",
        "    private void visit(TreeNode root, int leftMax, int rightMax) {\n",
        "        max = Math.max(max, Math.max(leftMax, rightMax));\n",
        "\n",
        "        if (root == null) {\n",
        "            return;\n",
        "        }\n",
        "\n",
        "        if (root.left != null) {\n",
        "            visit(root.left, rightMax + 1, 0);\n",
        "        }\n",
        "\n",
        "        if (root.right != null) {\n",
        "            visit(root.right, 0, leftMax + 1);\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d42e5a35",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=538 lang=java\n",
        " *\n",
        " * [538] 把二叉搜索树转换为累加树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode convertBST(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return root;\n",
        "    }\n",
        "    // 记录累加和\n",
        "    int sum = 0;\n",
        "    void traverse(TreeNode root){\n",
        "        //base case\n",
        "        if(root == null) return;\n",
        "        traverse(root.right);\n",
        "        \n",
        "         /** 中序位置 */\n",
        "        sum+= root.val;\n",
        "        //将BST 转化为累加树\n",
        "        root.val =sum;\n",
        "\n",
        "        traverse(root.left);\n",
        "      \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1986b298",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=669 lang=java\n",
        " *\n",
        " * [669] 修剪二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode trimBST(TreeNode root, int L, int R) {\n",
        "        //base case  1\n",
        "        if (root == null)\n",
        "            return root;\n",
        "         //base case  2\n",
        "        //下面两个if相当于删除不满足要求的节点\n",
        "        if (root.val < L)\n",
        "            return trimBST(root.right, L, R);//返回修剪过的右子树。抱有一丝丝期望，希望右子树能够满足要求，因为右子树的值大于当前根节点的值\n",
        "        if (root.val > R)\n",
        "            return trimBST(root.left, L, R);//返回修剪过的左子树，抱有一丝丝期望，希望左子树能够满足要求，因为左子树的值小于当前根节点的值\n",
        "\n",
        "        //处理正常的节点\n",
        "        root.left = trimBST(root.left, L, R);\n",
        "        root.right = trimBST(root.right, L, R);\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "dcd95afa",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=222 lang=java\n",
        " *\n",
        " * [222] 完全二叉树的节点个数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int sum = 0;\n",
        "    public int countNodes(TreeNode root) {\n",
        "            traverse(root);\n",
        "            return sum;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "\n",
        "        /** 前序 位置 */\n",
        "        sum +=1;\n",
        "         traverse(root.left);\n",
        "         if(root.right != null){\n",
        "             traverse(root.right);\n",
        "         }\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "04d36f4c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=96 lang=java\n",
        " *\n",
        " * [96] 不同的二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    //备忘录\n",
        "    int[][] memo;\n",
        "    public int numTrees(int n) {\n",
        "       \n",
        "        memo = new int[n+1][n+1];\n",
        "         //卡特兰数\n",
        "        return count(1,n);\n",
        "\n",
        "    }\n",
        "    /* 计算闭区间[lo,hi] 组成的BST个数 */\n",
        "    int count(int lo,int hi){\n",
        "       \n",
        "        //base case\n",
        "        if(lo > hi) return 1;\n",
        "        if(memo[lo][hi]!=0){\n",
        "            return memo[lo][hi];\n",
        "        }\n",
        "        int res = 0;\n",
        "        for(int i=lo;i<=hi;i++){\n",
        "            //i的值作为根节点 root\n",
        "            int left = count(lo,i-1);\n",
        "            int right = count(i+1,hi);\n",
        "            //左右子树的组合乘积是BST的总数\n",
        "            res +=left * right;\n",
        "        }\n",
        "        memo[lo][hi] = res;\n",
        "        return res;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "267b9239",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=153 lang=java\n",
        " *\n",
        " * [153] 寻找旋转排序数组中的最小值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int findMin(int[] nums) {\n",
        "       int low =0;\n",
        "       int high = nums.length -1;\n",
        "       while(low < high){\n",
        "           int pivot = low + (high - low) / 2;\n",
        "           if(nums[pivot] < nums[high]){\n",
        "               high = pivot;\n",
        "           }else{\n",
        "               low = pivot +1;\n",
        "           }\n",
        "       }   \n",
        "       return nums[low];  \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "fb474350",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=623 lang=java\n",
        " *\n",
        " * [623] 在二叉树中增加一行\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int dep = 1;\n",
        "    public TreeNode addOneRow(TreeNode root, int val, int depth) {\n",
        "            if(depth == 1){\n",
        "                TreeNode r = new TreeNode(val);\n",
        "                r.left = root;\n",
        "                return r;\n",
        "            }\n",
        "            return bts(root,val,depth);\n",
        "    }\n",
        "    TreeNode bts(TreeNode root, int val, int depth){\n",
        "        if(root == null) return null;\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "         q.add(root);\n",
        "      \n",
        "        while(!q.isEmpty()){\n",
        "            int sz = q.size();\n",
        "            for(int i=0;i<sz;i++){\n",
        "                TreeNode t = q.poll();\n",
        "                if(dep+1 == depth){\n",
        "                    TreeNode a = new TreeNode(val),\n",
        "                     b = new TreeNode(val);\n",
        "                    a.left = t.left;\n",
        "                    b.right = t.right;\n",
        "                    t.left = a; \n",
        "                    t.right = b;\n",
        "                }\n",
        "                if(t.left != null){\n",
        "                      q.add(t.left);     \n",
        "                 }\n",
        "                 if(t.right != null){\n",
        "                      q.add(t.right);     \n",
        "                 }\n",
        "   \n",
        "                }\n",
        "               dep ++;\n",
        "            }\n",
        "           return root; \n",
        "\n",
        "        }\n",
        "\n",
        "    }\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7a500229",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1367 lang=java\n",
        " *\n",
        " * [1367] 二叉树中的链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean isSubPath(ListNode head, TreeNode root) {\n",
        "        if(root==null) return false;\n",
        "        return dfs(head,root)||isSubPath(head,root.left)||isSubPath(head,root.right);\n",
        "    }\n",
        "\n",
        "    public boolean dfs(ListNode head,TreeNode root){\n",
        "        if(head==null) return true;\n",
        "        if(root==null) return false;\n",
        "        if(head.val!=root.val) return false;\n",
        "        return dfs(head.next,root.left)||dfs(head.next,root.right);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6090e945",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=605 lang=java\n",
        " *\n",
        " * [605] 种花问题\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean canPlaceFlowers(int[] flowerbed, int n) {\n",
        "        //跳格子\n",
        "        int i =0,len = flowerbed.length;\n",
        "        while(i<len && n >0){\n",
        "            if(flowerbed[i] == 1){\n",
        "                i+=2;\n",
        "            }else if(i == flowerbed.length-1 || flowerbed[i+1] == 0){\n",
        "                n--;\n",
        "                i +=2;\n",
        "            }else{\n",
        "                //flowerbed[i+1] =1\n",
        "                i +=3;\n",
        "            }\n",
        "        }\n",
        "        return n <= 0;\n",
        "\n",
        "        \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3fba0840",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=875 lang=java\n",
        " *\n",
        " * [875] 爱吃香蕉的珂珂\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int minEatingSpeed(int[] piles, int h) {\n",
        "        int maxVal =1;\n",
        "        for(int pile:piles){\n",
        "            maxVal = Math.max(maxVal,pile);\n",
        "        }\n",
        "        //速度最小的时候，耗时最长\n",
        "        int left =1;\n",
        "        //速度最大的时候，耗时最短\n",
        "        int right = maxVal;\n",
        "        while(left < right){\n",
        "            int mid = left +(right - left)/2;\n",
        "            if(cacluateSum(piles,mid) >h){\n",
        "                left = mid+1;\n",
        "            }else{\n",
        "                right = mid;\n",
        "            }\n",
        "        }\n",
        "        return left;\n",
        "    }\n",
        "    private int cacluateSum(int[] piles,int speed){\n",
        "        int sum = 0;\n",
        "        for(int pile:piles){\n",
        "            //向上取整\n",
        "            sum+= (pile +speed -1) /speed;\n",
        "        }\n",
        "        return sum;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b5a83476",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=998 lang=java\n",
        " *\n",
        " * [998] 最大二叉树 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode insertIntoMaxTree(TreeNode root, int val) {\n",
        "        TreeNode node = new TreeNode(val);\n",
        "        TreeNode prev = null, cur = root;\n",
        "        while (cur != null && cur.val > val) {\n",
        "            prev = cur; cur = cur.right;\n",
        "        }\n",
        "        if (prev == null) {\n",
        "            node.left = cur;\n",
        "            return node;\n",
        "        } else {\n",
        "            prev.right = node;\n",
        "            node.left = cur;\n",
        "            return root;\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2e0025b8",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=144 lang=java\n",
        " *\n",
        " * [144] 二叉树的前序遍历\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public List<Integer> preorderTraversal(TreeNode root) {\n",
        "        List<Integer> result = new ArrayList<>();\n",
        "        traverse(root,result);\n",
        "        return result;\n",
        "\n",
        "    }\n",
        "\n",
        "    public void traverse(TreeNode root,List<Integer> result){\n",
        "        if(root == null){\n",
        "            return;\n",
        "        }\n",
        "\n",
        "        result.add(root.val);\n",
        "        traverse(root.left,result);\n",
        "        traverse(root.right,result);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "26661a11",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=129 lang=java\n",
        " *\n",
        " * [129] 求根节点到叶节点数字之和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int sum =0;\n",
        "    StringBuilder sb = new StringBuilder();\n",
        "\n",
        "    public int sumNumbers(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return sum;\n",
        "    }\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "\n",
        "        /** 前序位置 */\n",
        "         sb.append(\"\"+root.val); \n",
        "        \n",
        "        //求和判断条件\n",
        "        if(root.left == null && root.right == null){         \n",
        "            sum += Integer.parseInt(sb.toString());\n",
        "        }\n",
        "        /** ------- */\n",
        "        traverse(root.left);\n",
        "        traverse(root.right);\n",
        "\n",
        "         /** 后序位置 */\n",
        "        sb.deleteCharAt(sb.length()-1);\n",
        "        /** ------- */\n",
        "\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e50e0976",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=101 lang=java\n",
        " *\n",
        " * [101] 对称二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "    public boolean isSymmetric(TreeNode root) {\n",
        "       \n",
        "        return  traverse(root,root);\n",
        "        \n",
        "    }\n",
        "\n",
        "     boolean traverse(TreeNode node1,TreeNode node2){\n",
        "       //base case\n",
        "       if(node1 == null && node2 == null) return true;\n",
        "\n",
        "       if(node1 == null || node2 == null) return false;\n",
        "\n",
        "        boolean f1 =  traverse(node1.left,node2.right);\n",
        "\n",
        "        boolean f2 =  traverse(node1.right,node2.left);\n",
        "\n",
        "        return f1 && f2 && (node1.val == node2.val);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3c989de7",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1318 lang=java\n",
        " *\n",
        " * [1318] 或运算的最小翻转次数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int minFlips(int a, int b, int c) {\n",
        "\n",
        "        int res = 0;\n",
        "        for(int i=0;i<30;i++){\n",
        "            int x =(a >> i) & 1,y = (b>>i) & 1,z = (c>>i) & 1;\n",
        "            if(z ==0){\n",
        "                res += x+y;\n",
        "            }else{\n",
        "                res +=(x|y)^1;\n",
        "            }\n",
        "        }\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "848c8a29",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=894 lang=java\n",
        " *\n",
        " * [894] 所有可能的真二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public List<TreeNode> allPossibleFBT(int N) {\n",
        "\n",
        "        List<TreeNode> possibleFBT= new ArrayList<>();\n",
        "        // 如果是偶数，则不存在满二叉树\n",
        "        if (N % 2 == 0){\n",
        "            return possibleFBT;\n",
        "        }\n",
        "        // 如果 N == 1 那只有一个node，直接创建返回即可\n",
        "        if (N == 1){\n",
        "            possibleFBT.add(new TreeNode(0));\n",
        "        }\n",
        "        // 如果N > 1，则去掉根节点，即 N = N - 1\n",
        "        N --;\n",
        "        /**\n",
        "         * 对于7来说，去掉根节点，则可以分为\n",
        "         * 1 + 5\n",
        "         * 3 + 3\n",
        "         * 5 + 1\n",
        "         *\n",
        "         * 5作为子树，去掉根节点又可分为\n",
        "         * 1 + 3\n",
        "         * 3 + 1\n",
        "         *\n",
        "         * 3作为子树，去掉根节点又可分为\n",
        "         * 1 + 1\n",
        "         *\n",
        "         * 所以 7 可以得到的满二叉树有\n",
        "         * 1 + 5(1 + 3(1 + 1))\n",
        "         * 1 + 5(3(1 + 1) + 1)\n",
        "         * 3(1 + 1) + 3(1 + 1)\n",
        "         * 5(1 + 3(1 + 1)) + 1\n",
        "         * 5(3(1 + 1) + 1) + 1\n",
        "         */\n",
        "\n",
        "        for (int i = 1; i < N; i += 2){\n",
        "            List<TreeNode> leftTree = allPossibleFBT(i);\n",
        "            List<TreeNode> rightTree = allPossibleFBT(N - i);\n",
        "            for (TreeNode lnode : leftTree){\n",
        "                for (TreeNode rnode : rightTree){\n",
        "                    TreeNode root = new TreeNode(0);\n",
        "                    root.left = lnode;\n",
        "                    root.right = rnode;\n",
        "                    possibleFBT.add(root);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return possibleFBT;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2b60d420",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=283 lang=java\n",
        " *\n",
        " * [283] 移动零\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public void moveZeroes(int[] nums) {\n",
        "        if(nums == null){\n",
        "            return;\n",
        "        }\n",
        "        //将非零的移到左边，并记录最后一个非零的下标j\n",
        "        int j=0;\n",
        "        for(int i=0;i<nums.length;i++){\n",
        "            if(nums[i]!=0){\n",
        "                nums[j++] = nums[i];\n",
        "            }\n",
        "        }\n",
        "        \n",
        "        for(int i=j;i<nums.length;i++){\n",
        "            nums[i] =0;\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "995ac43d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=700 lang=java\n",
        " *\n",
        " * [700] 二叉搜索树中的搜索\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode searchBST(TreeNode root, int val) {\n",
        "        if(root == null) return null;\n",
        "        if(root.val > val){\n",
        "            return searchBST(root.left,val);\n",
        "        }else if(root.val < val){\n",
        "            return searchBST(root.right,val);\n",
        "        }\n",
        "        return root;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c423661e",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=234 lang=java\n",
        " *\n",
        " * [234] 回文链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    // 左侧指针\n",
        "ListNode left;\n",
        "\n",
        "boolean isPalindrome(ListNode head) {\n",
        "    left = head;\n",
        "    return traverse(head);\n",
        "}\n",
        "\n",
        "boolean traverse(ListNode right) {\n",
        "    if (right == null) return true;\n",
        "    boolean res = traverse(right.next);\n",
        "    // 后序遍历代码\n",
        "    res = res && (right.val == left.val);\n",
        "    left = left.next;\n",
        "    return res;\n",
        "}\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a6a64a27",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2130 lang=java\n",
        " *\n",
        " * [2130] 链表最大孪生和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public int pairSum(ListNode head) {\n",
        "    Stack<ListNode> stack = new Stack<>();\n",
        "    ListNode curNode = head;\n",
        "    while(curNode!=null){\n",
        "        stack.push(curNode);\n",
        "        curNode  =curNode.next;\n",
        "    }\n",
        "\n",
        "    int max = 0;\n",
        "    int n = stack.size()/2;\n",
        "    for(int i=0;i<n;i++){\n",
        "        max = Math.max(max,stack.pop().val +head.val);\n",
        "        head = head.next;\n",
        "    }\n",
        "    return max;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5262d0f7",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=530 lang=java\n",
        " *\n",
        " * [530] 二叉搜索树的最小绝对差\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    TreeNode pre = null;\n",
        "    int min = Integer.MAX_VALUE;\n",
        "    public int getMinimumDifference(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return min;\n",
        "    }\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "\n",
        "        traverse(root.left);\n",
        "        /** 中序位置 */\n",
        "        if(pre != null){\n",
        "            int diff = Math.abs(root.val-pre.val);\n",
        "            if(diff < min){\n",
        "                min =diff;\n",
        "            }\n",
        "        }\n",
        "        pre = root;\n",
        "\n",
        "        traverse(root.right);\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "701c6f7e",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=206 lang=java\n",
        " *\n",
        " * [206] 反转链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode reverseList(ListNode head) {\n",
        "     ListNode prev = null;\n",
        "     ListNode curr = head;\n",
        "     while(curr !=null){\n",
        "         ListNode next = curr.next;\n",
        "         curr.next = prev;\n",
        "         prev =curr;\n",
        "         curr  =next;\n",
        "     }\n",
        "     return prev;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5592a15c",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1926 lang=java\n",
        " *\n",
        " * [1926] 迷宫中离入口最近的出口\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int nearestExit(char[][] maze, int[] entrance) {\n",
        "       int m  = maze.length;\n",
        "       int n = maze[0].length;\n",
        "       int[][] directions = {{0,1,0,-1},{1,0,-1,0}}; \n",
        "\n",
        "       //记录已经访问过的点坐标\n",
        "       Queue<int[]> queue = new LinkedList<>();\n",
        "       //将入口加入队列\n",
        "       queue.offer(new int[]{entrance[0],entrance[1],0});//最后一位表示当前走了几步\n",
        "       //遍历到坐标变为墙  防止重复遍历\n",
        "       maze[entrance[0]][entrance[1]]  = '+';\n",
        "\n",
        "       while(!queue.isEmpty()){\n",
        "           int[] curLocal  =queue.poll();//坐标出队\n",
        "           int row = curLocal[0];\n",
        "           int col  =curLocal[1];\n",
        "           for(int i=0;i<4;i++){\n",
        "               int x = row +directions[0][i];\n",
        "               int y = col +directions[1][i];\n",
        "               //新坐标合法 且 不为墙\n",
        "               if(x>=0 && x <=m-1 && y>=0 && y<=n-1&&maze[x][y] == '.'){\n",
        "                   //新坐标为出口\n",
        "                   if(x == 0 || x == m -1 || y==0||y==n-1){\n",
        "                       return curLocal[2] +1; //多迈一步才能走出迷宫\n",
        "                   }\n",
        "                   queue.offer(new int[]{x,y,curLocal[2] +1});\n",
        "                   maze[x][y]='+';\n",
        "               }\n",
        "           }\n",
        "       }\n",
        "       return -1;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d586ac4e",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=110 lang=java\n",
        " *\n",
        " * [110] 平衡二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean isBalanced(TreeNode root) {\n",
        "        \n",
        "        return getHeight(root)>=0;\n",
        "\n",
        "    }\n",
        "    int getHeight(TreeNode root){\n",
        "        if(root == null) return 0;\n",
        "\n",
        "        int left = getHeight(root.left);\n",
        "        int right = getHeight(root.right);\n",
        "\n",
        "        // 后序位置 判断 是否平衡，不平衡返回 -1\n",
        "       if (left == -1 || right == -1 || Math.abs(left - right) > 1) {\n",
        "           // 说明存在不平衡子树，只需要把 -1 自底向上传递即可。\n",
        "            return -1;\n",
        "        } else {\n",
        "            return Math.max(left, right) + 1;\n",
        "        }\n",
        "\n",
        "    }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0b264813",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=15 lang=java\n",
        " *\n",
        " * [15] 三数之和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<Integer>> threeSum(int[] nums) {\n",
        "         int lo =start ;\n",
        "            int hi =nums.length-1;\n",
        "          while(lo < hi){\n",
        "              int left = nums[lo];\n",
        "              int right = nums[hi];\n",
        "              int sum = nums[lo]+nums[hi];\n",
        "              if(sum > target){\n",
        "                  //跳过所有重复元素\n",
        "                  while(lo <hi && nums[hi] == right ) hi --;\n",
        "              }else if(sum < target){\n",
        "                //跳过所有重复元素\n",
        "                  while(lo < hi && nums[lo] == left ) lo ++;\n",
        "              }else if(sum == target){\n",
        "                  List<Integer>  eq = new ArrayList<>(3);\n",
        "                  eq.add(cur);\n",
        "                  eq.add(left);\n",
        "                  eq.add(right);\n",
        "                  result.add(eq);\n",
        "                  //跳过所有重复元素\n",
        "                  while(lo < hi && nums[lo] == left ) lo ++;\n",
        "                  while(lo <hi && nums[hi] == right ) hi --;\n",
        "              }\n",
        "    }\n",
        "\n",
        "     public List<List<Integer>> threeSum(int[] nums) {\n",
        "\n",
        "        List<List<Integer>> result = new ArrayList<>();\n",
        "        //1.排序\n",
        "        Arrays.sort(nums);\n",
        "        int size = nums.length;\n",
        "        for(int i=0;i<size;i++){\n",
        "            int cur = nums[i];\n",
        "            int target = 0 - cur;\n",
        "            twoSum(nums[i],nums,i+1,target,result);   \n",
        "            \n",
        "            //跳过第一个数字重复的情况，否则会出现重复数据\n",
        "            while(i<size-1 && nums[i] == nums[i+1]) i++;\n",
        "        }\n",
        "\n",
        "        return result;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "868aa7c6",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2095 lang=java\n",
        " *\n",
        " * [2095] 删除链表的中间节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode deleteMiddle(ListNode head) {\n",
        "        ListNode slow = head;\n",
        "        ListNode fast = head;\n",
        "        ListNode delPreNode = null;\n",
        "        while(fast!=null && fast.next!=null){\n",
        "            delPreNode = slow;\n",
        "            slow = slow.next;\n",
        "            fast = fast.next.next;\n",
        "        }\n",
        "        if(delPreNode == null){\n",
        "            return null;\n",
        "        }\n",
        "        delPreNode.next  =delPreNode.next.next;\n",
        "        return head;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "b88c4eea",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1584 lang=java\n",
        " *\n",
        " * [1584] 连接所有点的最小费用\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int minCostConnectPoints(int[][] points) {\n",
        "        int n = points.length;\n",
        "        List<int[]> edges = new ArrayList<>();\n",
        "        for(int i=0;i<n;i++){\n",
        "            for(int j=i+1;j<n;j++){\n",
        "                int xi = points[i][0],yi = points[i][1];\n",
        "                int xj = points[j][0],yj=points[j][1];\n",
        "                edges.add(\n",
        "                    new int[]{\n",
        "                        i,j,Math.abs(xi - xj) +Math.abs(yi - yj)\n",
        "                    }\n",
        "                );\n",
        "            }\n",
        "        }\n",
        "        Collections.sort(edges,(a,b) ->{\n",
        "             return a[2]-b[2];\n",
        "        });\n",
        "\n",
        "        //执行Kruskal算法\n",
        "        int mst = 0;\n",
        "        UF uf = new UF(n);\n",
        "        for(int[] edge:edges){\n",
        "            int u = edge[0];\n",
        "            int v = edge[1];\n",
        "            int weight = edge[2];\n",
        "            if(uf.connect(u,v)){\n",
        "                continue;\n",
        "            }\n",
        "            //若这条边产生环，则不能加入mst\n",
        "            mst += weight;\n",
        "            uf.union(u,v);\n",
        "        }\n",
        "        return mst;\n",
        "\n",
        "    }\n",
        "}\n",
        "class UF{\n",
        "   private int count;\n",
        "   private int[] parent;\n",
        "\n",
        "   public UF(int n){\n",
        "    this.count =n;\n",
        "    this.parent = new int[n];\n",
        "    for(int i=0;i<n;i++){\n",
        "        parent[i] = i;\n",
        "    }\n",
        "   }\n",
        "\n",
        "   public int find(int x){\n",
        "        if(parent[x]!=x){\n",
        "            parent[x] =find(parent[x]);\n",
        "        }\n",
        "        return parent[x];\n",
        "   }\n",
        "   public void union(int q,int p){\n",
        "    int rootp = find(p);\n",
        "    int rootq = find(q);\n",
        "    if(rootp == rootq) return;\n",
        "    parent[rootp] = rootq;\n",
        "    this.count --;\n",
        "   }\n",
        "   public boolean connect(int q,int p){\n",
        "    int rootp = find(p);\n",
        "    int rootq = find(q);\n",
        "    return rootp == rootq;\n",
        "   }\n",
        "\n",
        "   public int count(){\n",
        "    return this.count;\n",
        "   }\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7f399b1d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=872 lang=java\n",
        " *\n",
        " * [872] 叶子相似的树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "\n",
        "    public boolean leafSimilar(TreeNode root1, TreeNode root2) {\n",
        "\n",
        "         List<Integer> res1 = new ArrayList<>();\n",
        "         List<Integer> res2 = new ArrayList<>();\n",
        "            traverse(root1,res1);\n",
        "            traverse(root2,res2);\n",
        "        return res1.equals(res2);\n",
        "    }\n",
        "    void traverse(TreeNode root,List<Integer> res){\n",
        "        if(root == null) return;\n",
        "\n",
        "        if(root.left == null && root.right == null ){\n",
        "            res.add(root.val);\n",
        "            return;\n",
        "        }\n",
        "\n",
        "        traverse(root.left,res);\n",
        "        traverse(root.right,res);\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "71ee6aa6",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=72 lang=java\n",
        " *\n",
        " * [72] 编辑距离\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int minDistance(String word1, String word2) {\n",
        "        int n = word1.length();\n",
        "        int m = word2.length();\n",
        "\n",
        "        //有一个字符串为空串\n",
        "        if(n * m == 0){\n",
        "            return n+m;\n",
        "        }\n",
        "\n",
        "        //DP 数组\n",
        "        int[][] D = new int[n+1][m+1];\n",
        "\n",
        "        //边界状态初始化\n",
        "        for(int i=0;i<n+1;i++){\n",
        "            D[i][0] = i;\n",
        "        }\n",
        "\n",
        "        for(int j =0;j<m+1; j++){\n",
        "            D[0][j] = j;\n",
        "        }\n",
        "\n",
        "        //计算所有DP值\n",
        "        for(int i = 1;i<n+1;i++){\n",
        "            for(int j =1;j<m+1;j++){\n",
        "                int left =D[i -1][j] +1;\n",
        "                int down = D[i][j-1]+1;\n",
        "                int left_down = D[i-1][j-1];\n",
        "                if(word1.charAt(i-1) != word2.charAt(j-1)){\n",
        "                    left_down +=1;\n",
        "                }\n",
        "                D[i][j] = Math.min(left,Math.min(down,left_down));\n",
        "            }\n",
        "        }\n",
        "        return D[n][m];\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "87b9f367",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1315 lang=java\n",
        " *\n",
        " * [1315] 祖父节点值为偶数的节点和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int res;\n",
        "    public int sumEvenGrandparent(TreeNode root) {\n",
        "        \n",
        "        res = 0;\n",
        "        helper(null, null, root);\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    private void helper(TreeNode grandP, TreeNode father, TreeNode node) {\n",
        "        if(node == null) return;\n",
        "\n",
        "        if(grandP != null && grandP.val % 2 == 0) {\n",
        "            res += node.val;\n",
        "        }\n",
        "        \n",
        "        helper(father, node, node.left);\n",
        "        helper(father, node, node.right);\n",
        "    }\n",
        "}\n",
        "\n",
        "\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "318de105",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=53 lang=java\n",
        " *\n",
        " * [53] 最大子数组和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int maxSubArray(int[] nums) {\n",
        "        //滑动窗口\n",
        "        // 窗口内元素之和大于等于 0 时扩大窗口，在窗口内元素之和小于 0 时缩小窗口，在每次移动窗口时更新答案\n",
        "        int left =0,right =0;\n",
        "        int windowSum = 0,maxSum = Integer.MIN_VALUE;\n",
        "        while(right < nums.length){\n",
        "            //扩大窗口并更新窗口内的元素和\n",
        "            windowSum += nums[right];\n",
        "            right ++;\n",
        "\n",
        "            //更新答案\n",
        "            maxSum = windowSum > maxSum ? windowSum:maxSum;\n",
        "\n",
        "            //判断窗口是否要收缩\n",
        "            while(windowSum < 0){\n",
        "                windowSum -= nums[left];\n",
        "                left ++;\n",
        "            }\n",
        "        }\n",
        "        return maxSum;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4b2a7761",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=919 lang=java\n",
        " *\n",
        " * [919] 完全二叉树插入器\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class CBTInserter {\n",
        "    Queue<TreeNode> candidate;\n",
        "    TreeNode root;\n",
        "\n",
        "    public CBTInserter(TreeNode root) {\n",
        "        this.candidate = new ArrayDeque<TreeNode>();\n",
        "        this.root = root;\n",
        "        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();\n",
        "        queue.offer(root);\n",
        "\n",
        "        while (!queue.isEmpty()) {\n",
        "           TreeNode node = queue.poll();\n",
        "            if (node.left != null) {\n",
        "                queue.offer(node.left);\n",
        "            }\n",
        "            if (node.right != null) {\n",
        "                queue.offer(node.right);\n",
        "            }\n",
        "            if (!(node.left != null && node.right != null)) {\n",
        "                candidate.offer(node);\n",
        "            }\n",
        "        }\n",
        "\n",
        "\n",
        "    }\n",
        "    \n",
        "    public int insert(int val) {\n",
        "        TreeNode child = new TreeNode(val);\n",
        "        TreeNode node = candidate.peek();\n",
        "        int ret = node.val;\n",
        "        if (node.left == null) {\n",
        "            node.left = child;\n",
        "        } else {\n",
        "            node.right = child;\n",
        "            candidate.poll();\n",
        "        }\n",
        "        candidate.offer(child);\n",
        "        return ret;\n",
        "    }\n",
        "    \n",
        "    public TreeNode get_root() {\n",
        "        return this.root;\n",
        "    }\n",
        "\n",
        "\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your CBTInserter object will be instantiated and called as such:\n",
        " * CBTInserter obj = new CBTInserter(root);\n",
        " * int param_1 = obj.insert(val);\n",
        " * TreeNode param_2 = obj.get_root();\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "502dec39",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1026 lang=java\n",
        " *\n",
        " * [1026] 节点与其祖先之间的最大差值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public int maxAncestorDiff(TreeNode root) {\n",
        "        return dfs(root, root.val, root.val);\n",
        "    }\n",
        "\n",
        "    public int dfs(TreeNode root, int mi, int ma) {\n",
        "        if (root == null) {\n",
        "            return 0;\n",
        "        }\n",
        "        int diff = Math.max(Math.abs(root.val - mi), Math.abs(root.val - ma));\n",
        "        mi = Math.min(mi, root.val);\n",
        "        ma = Math.max(ma, root.val);\n",
        "        diff = Math.max(diff, dfs(root.left, mi, ma));\n",
        "        diff = Math.max(diff, dfs(root.right, mi, ma));\n",
        "        return diff;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "50e8d609",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=94 lang=java\n",
        " *\n",
        " * [94] 二叉树的中序遍历\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public List<Integer> inorderTraversal(TreeNode root) {\n",
        "        List<Integer> result = new ArrayList<>();\n",
        "        traverse(root,result);\n",
        "        return result;\n",
        "\n",
        "    }\n",
        "\n",
        "    public void traverse(TreeNode root,List<Integer> result){\n",
        "        //base case\n",
        "        if(root == null) return ;\n",
        "\n",
        "        traverse(root.left,result);\n",
        "        //\n",
        "        result.add(root.val);\n",
        "\n",
        "        traverse(root.right,result);\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2d752c06",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=111 lang=java\n",
        " *\n",
        " * [111] 二叉树的最小深度\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " * 使用BFS框架 找到最短。。。\n",
        " */\n",
        "class Solution {\n",
        "    public int minDepth(TreeNode root) {\n",
        "        if(root == null) return 0;\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "        int depth =1;\n",
        "\n",
        "        while(q!= null){\n",
        "            //sz 在while 循环计算\n",
        "            int sz = q.size();\n",
        "\n",
        "            for(int i=0; i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                //判断是否到达终点\n",
        "                if(cur.left == null && cur.right == null){\n",
        "                    return depth;\n",
        "                }\n",
        "\n",
        "                if(cur.left !=null){\n",
        "                    q.offer(cur.left);\n",
        "                }\n",
        "                if(cur.right != null){\n",
        "                    q.offer(cur.right);\n",
        "                }\n",
        "\n",
        "            }\n",
        "            depth++;\n",
        "\n",
        "        }\n",
        "        return depth;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "62032ecd",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=139 lang=java\n",
        " *\n",
        " * [139] 单词拆分\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean wordBreak(String s, List<String> wordDict) {\n",
        "        Set<String> wordDictSet = new HashSet(wordDict);\n",
        "        boolean[] dp = new boolean[s.length()+1];\n",
        "        dp[0] = true;\n",
        "        for(int i = 1; i<= s.length();i++){\n",
        "            for(int j = 0; j< i; j++){\n",
        "                if(dp[j] && wordDict.contains(s.substring(j,i))){\n",
        "                    dp[i] = true;\n",
        "                    break;\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return dp[s.length()];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4a3a2255",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=547 lang=java\n",
        " *\n",
        " * [547] 省份数量\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int findCircleNum(int[][] isConnected) {\n",
        "        int cities = isConnected.length;\n",
        "        boolean[] visited = new boolean[cities];\n",
        "        int provinces = 0;\n",
        "\n",
        "        Queue<Integer> queue = new LinkedList<Integer>();\n",
        "        for(int i =0;i<cities;i++){\n",
        "            if(!visited[i]){\n",
        "                queue.offer(i);\n",
        "                while(!queue.isEmpty()){\n",
        "                    int j = queue.poll();\n",
        "                    visited[j] = true;\n",
        "                    for(int k=0;k<cities;k++){\n",
        "                        if(isConnected[j][k] ==1 && !visited[k]){\n",
        "                            queue.offer(k);\n",
        "                        }\n",
        "                    }\n",
        "                }\n",
        "                provinces ++;\n",
        "            }\n",
        "        }\n",
        "        return provinces;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d94f1414",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1 lang=java\n",
        " *\n",
        " * [1] 两数之和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] twoSum(int[] nums, int target) {\n",
        "        int[] result = new int[]{-1,-1};\n",
        "            for(int i=0;i<nums.length;i++){   \n",
        "                    int left =i ;\n",
        "                    int right =nums.length-1;\n",
        "                while(left < right){\n",
        "                    if(nums[left]+nums[right] == target){\n",
        "                        result[0] = left;\n",
        "                        result[1]=right;\n",
        "                        break;\n",
        "                    }\n",
        "                    right --;\n",
        "                }\n",
        "\n",
        "            }\n",
        "            return result;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "37c0a0b4",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1339 lang=java\n",
        " *\n",
        " * [1339] 分裂二叉树的最大乘积\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int avg, half; // avg:整棵树总值的中间值 half:真正的中间值\n",
        "    public int maxProduct(TreeNode root) {\n",
        "        int total = getTotal(root); // 获取这棵树总值\n",
        "        avg = total / 2;\n",
        "        half = total;\n",
        "        // 接下来我们不断优化half 使它不断接近整棵树一半的值\n",
        "        getTotal(root);\n",
        "        return (int) ((long) half * (total - half) % (1e9 + 7));\n",
        "    }\n",
        "    int getTotal(TreeNode root) {\n",
        "        if (root == null) return 0;\n",
        "        int val = root.val + getTotal(root.left) + getTotal(root.right);\n",
        "        // 条件成立：代表遍历到当前节点 对比其它节点 更接近中间值\n",
        "        if (Math.abs(val - avg) < Math.abs(half - avg)) half = val; // 更新half\n",
        "        return val;\n",
        "    }\n",
        "}\n",
        "\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4f339b39",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=117 lang=java\n",
        " *\n",
        " * [117] 填充每个节点的下一个右侧节点指针 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/*\n",
        "// Definition for a Node.\n",
        "class Node {\n",
        "    public int val;\n",
        "    public Node left;\n",
        "    public Node right;\n",
        "    public Node next;\n",
        "\n",
        "    public Node() {}\n",
        "    \n",
        "    public Node(int _val) {\n",
        "        val = _val;\n",
        "    }\n",
        "\n",
        "    public Node(int _val, Node _left, Node _right, Node _next) {\n",
        "        val = _val;\n",
        "        left = _left;\n",
        "        right = _right;\n",
        "        next = _next;\n",
        "    }\n",
        "};\n",
        "*/\n",
        "\n",
        "class Solution {\n",
        "    public Node connect(Node root) {\n",
        "        if(root == null) return null;\n",
        "        traverse(root);\n",
        "        return root;\n",
        "    }\n",
        "  void traverse(Node root){\n",
        "        if (root == null) return;\n",
        "        // 初始化队列，将 root 加入队列\n",
        "        Queue<Node> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "        \n",
        "        while (!q.isEmpty()) {\n",
        "        Node tmp = null;\n",
        "        int sz = q.size();\n",
        "        for(int i=0;i<sz;i++){\n",
        "            Node cur = q.poll();\n",
        "            \n",
        "            if(tmp != null){\n",
        "              \n",
        "                tmp.next = cur;\n",
        "            }\n",
        "            \n",
        "            tmp = cur;\n",
        "\n",
        "            if (cur.left != null) {\n",
        "                 q.offer(cur.left);\n",
        "            }\n",
        "            if (cur.right != null) {\n",
        "                q.offer(cur.right);\n",
        "            }\n",
        "        }\n",
        "         \n",
        "    }\n",
        "}\n",
        "\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "d4eece93",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=146 lang=java\n",
        " *\n",
        " * [146] LRU 缓存\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "public class LRUCache {\n",
        "    class DLinkedNode {\n",
        "        int key;\n",
        "        int value;\n",
        "        DLinkedNode prev;\n",
        "        DLinkedNode next;\n",
        "        public DLinkedNode() {}\n",
        "        public DLinkedNode(int _key, int _value) {key = _key; value = _value;}\n",
        "    }\n",
        "\n",
        "    private Map<Integer, DLinkedNode> cache = new HashMap<Integer, DLinkedNode>();\n",
        "    private int size;\n",
        "    private int capacity;\n",
        "    private DLinkedNode head, tail;\n",
        "\n",
        "    public LRUCache(int capacity) {\n",
        "        this.size = 0;\n",
        "        this.capacity = capacity;\n",
        "        // 使用伪头部和伪尾部节点\n",
        "        head = new DLinkedNode();\n",
        "        tail = new DLinkedNode();\n",
        "        head.next = tail;\n",
        "        tail.prev = head;\n",
        "    }\n",
        "\n",
        "    public int get(int key) {\n",
        "        DLinkedNode node = cache.get(key);\n",
        "        if (node == null) {\n",
        "            return -1;\n",
        "        }\n",
        "        // 如果 key 存在，先通过哈希表定位，再移到头部\n",
        "        moveToHead(node);\n",
        "        return node.value;\n",
        "    }\n",
        "\n",
        "    public void put(int key, int value) {\n",
        "        DLinkedNode node = cache.get(key);\n",
        "        if (node == null) {\n",
        "            // 如果 key 不存在，创建一个新的节点\n",
        "            DLinkedNode newNode = new DLinkedNode(key, value);\n",
        "            // 添加进哈希表\n",
        "            cache.put(key, newNode);\n",
        "            // 添加至双向链表的头部\n",
        "            addToHead(newNode);\n",
        "            ++size;\n",
        "            if (size > capacity) {\n",
        "                // 如果超出容量，删除双向链表的尾部节点\n",
        "                DLinkedNode tail = removeTail();\n",
        "                // 删除哈希表中对应的项\n",
        "                cache.remove(tail.key);\n",
        "                --size;\n",
        "            }\n",
        "        }\n",
        "        else {\n",
        "            // 如果 key 存在，先通过哈希表定位，再修改 value，并移到头部\n",
        "            node.value = value;\n",
        "            moveToHead(node);\n",
        "        }\n",
        "    }\n",
        "\n",
        "    private void addToHead(DLinkedNode node) {\n",
        "        node.prev = head;\n",
        "        node.next = head.next;\n",
        "        head.next.prev = node;\n",
        "        head.next = node;\n",
        "    }\n",
        "\n",
        "    private void removeNode(DLinkedNode node) {\n",
        "        node.prev.next = node.next;\n",
        "        node.next.prev = node.prev;\n",
        "    }\n",
        "\n",
        "    private void moveToHead(DLinkedNode node) {\n",
        "        removeNode(node);\n",
        "        addToHead(node);\n",
        "    }\n",
        "\n",
        "    private DLinkedNode removeTail() {\n",
        "        DLinkedNode res = tail.prev;\n",
        "        removeNode(res);\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "\n",
        "\n",
        "\n",
        "/**\n",
        " * Your LRUCache object will be instantiated and called as such:\n",
        " * LRUCache obj = new LRUCache(capacity);\n",
        " * int param_1 = obj.get(key);\n",
        " * obj.put(key,value);\n",
        " */\n",
        "\n",
        "/**\n",
        " * Your LRUCache object will be instantiated and called as such:\n",
        " * LRUCache obj = new LRUCache(capacity);\n",
        " * int param_1 = obj.get(key);\n",
        " * obj.put(key,value);\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a388d980",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=746 lang=java\n",
        " *\n",
        " * [746] 使用最小花费爬楼梯\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int minCostClimbingStairs(int[] cost) {\n",
        "        int n = cost.length;\n",
        "        int[] dp = new int[n+1];\n",
        "        dp[0] = dp[1] =0;\n",
        "        for(int i =2;i<=n;i++){\n",
        "            dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);\n",
        "        }\n",
        "        return dp[n];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "f455672a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=443 lang=java\n",
        " *\n",
        " * [443] 压缩字符串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int compress(char[] chars) {\n",
        "        int n = chars.length;\n",
        "        int write = 0, left = 0;\n",
        "        for(int read = 0;read < n ; read ++){\n",
        "            if(read == n-1 || chars[read] != chars[read +1]){\n",
        "                chars[write++] =chars[read];\n",
        "                //字符长度\n",
        "                int num = read-left + 1;\n",
        "                //\n",
        "                if(num > 1){\n",
        "                    int anchor  =write;\n",
        "                    while(num > 0){\n",
        "                        chars[write++] = (char)(num % 10 + '0');\n",
        "                        num /= 10;\n",
        "                    }\n",
        "                    //采用短除法将子串长度倒序写入原字符串中，然后再将其反转即可\n",
        "                    reverse(chars,anchor,write - 1);\n",
        "                }\n",
        "                left  =read + 1;\n",
        "            }\n",
        "        }\n",
        "        return write;\n",
        "    }\n",
        "\n",
        "    public void reverse(char[] chars,int left,int right){\n",
        "        while(left < right){\n",
        "            char temp = chars[left];\n",
        "            chars[left]  = chars[right];\n",
        "            chars[right] = temp;\n",
        "            left++;\n",
        "            right--;\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "14ea7abf",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=10 lang=java\n",
        " *\n",
        " * [10] 正则表达式匹配\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "     //备忘录\n",
        "     private int[][] memo;\n",
        "    public boolean isMatch(String s, String p) {\n",
        "       int m = s.length(),n = p.length();\n",
        "       memo = new int[m][n];\n",
        "       for(int[] row : memo){\n",
        "           Arrays.fill(row,-1); \n",
        "       }\n",
        "       //指针i,j 从索引0开始移动\n",
        "       return dp(s,0,p,0);\n",
        "\n",
        "    }\n",
        "    private boolean dp(String s,int i,String p,int j){\n",
        "        int m =s.length(),n =p.length();\n",
        "        //base case\n",
        "        if(j == n){\n",
        "            return i == m;\n",
        "        }\n",
        "        if(i == m){\n",
        "            if((n-j)%2 ==1){\n",
        "                return false;\n",
        "            }\n",
        "        \n",
        "            for(;j+1 < n;j+=2){\n",
        "                if(p.charAt(j+1)!='*'){\n",
        "                    return false;\n",
        "                }\n",
        "            }\n",
        "        return true;\n",
        "\n",
        "        }\n",
        "        // 查询备忘录\n",
        "        if(memo[i][j]!=-1){\n",
        "            return memo[i][j] == 1;\n",
        "        }\n",
        "        boolean res = false;\n",
        "        if(s.charAt(i) == p.charAt(j)||p.charAt(j)=='.'){\n",
        "            // p[j+1] == * 情况：\n",
        "            if(j<n-1&&p.charAt(j+1)=='*'){\n",
        "                res = dp(s, i, p, j+2)|| dp(s,i+1,p,j);\n",
        "            }else{\n",
        "                res = dp(s, i+1, p, j+1);\n",
        "            }\n",
        "        }else{\n",
        "            // p[j+1] == * 情况：\n",
        "            if(j<n-1&&p.charAt(j+1)=='*'){\n",
        "                res = dp(s, i, p, j+2);\n",
        "            }else{\n",
        "                res = false;\n",
        "            }\n",
        "        }\n",
        "        //将当前结果记入备忘录\n",
        "        memo[i][j] = res?1:0;\n",
        "        return res;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5cd2b476",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=338 lang=java\n",
        " *\n",
        " * [338] 比特位计数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int[] countBits(int n) {\n",
        "        int[] ans =new int[n+1];\n",
        "        for(int i=0;i<=n;i++){\n",
        "            ans[i] =countOnes(i);\n",
        "        }\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public int countOnes(int x){\n",
        "        int ones = 0;\n",
        "        while(x >0){\n",
        "            x &=(x-1);\n",
        "            ones++;\n",
        "        }\n",
        "        return ones;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1e89ffe8",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=785 lang=java\n",
        " *\n",
        " * [785] 判断二分图\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "   // 记录图是否符合二分图性质\n",
        "private boolean ok = true;\n",
        "// 记录图中节点的颜色，false 和 true 代表两种不同颜色\n",
        "private boolean[] color;\n",
        "// 记录图中节点是否被访问过\n",
        "private boolean[] visited;\n",
        "\n",
        "// 主函数，输入邻接表，判断是否是二分图\n",
        "public boolean isBipartite(int[][] graph) {\n",
        "    int n = graph.length;\n",
        "    color =  new boolean[n];\n",
        "    visited =  new boolean[n];\n",
        "    // 因为图不一定是联通的，可能存在多个子图\n",
        "    // 所以要把每个节点都作为起点进行一次遍历\n",
        "    // 如果发现任何一个子图不是二分图，整幅图都不算二分图\n",
        "    for (int v = 0; v < n; v++) {\n",
        "        if (!visited[v]) {\n",
        "            traverse(graph, v);\n",
        "        }\n",
        "    }\n",
        "    return ok;\n",
        "}\n",
        "\n",
        "// DFS 遍历框架\n",
        "private void traverse(int[][] graph, int v) {\n",
        "    // 如果已经确定不是二分图了，就不用浪费时间再递归遍历了\n",
        "    if (!ok) return;\n",
        "\n",
        "    visited[v] = true;\n",
        "    for (int w : graph[v]) {\n",
        "        if (!visited[w]) {\n",
        "            // 相邻节点 w 没有被访问过\n",
        "            // 那么应该给节点 w 涂上和节点 v 不同的颜色\n",
        "            color[w] = !color[v];\n",
        "            // 继续遍历 w\n",
        "            traverse(graph, w);\n",
        "        } else {\n",
        "            // 相邻节点 w 已经被访问过\n",
        "            // 根据 v 和 w 的颜色判断是否是二分图\n",
        "            if (color[w] == color[v]) {\n",
        "                // 若相同，则此图不是二分图\n",
        "                ok = false;\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "}\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3a444951",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=783 lang=java\n",
        " *\n",
        " * [783] 二叉搜索树节点最小距离\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int diff =Integer.MAX_VALUE;\n",
        "    TreeNode pre = null;\n",
        "    public int minDiffInBST(TreeNode root) {\n",
        "        traverse(root);\n",
        "        return diff;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "        \n",
        "        traverse(root.left);\n",
        "        /** 中序位置 */\n",
        "        if(pre != null){\n",
        "            \n",
        "            int d = Math.abs(root.val - pre.val);\n",
        "            \n",
        "            diff = Math.min(diff,d);\n",
        "        }\n",
        "        pre = root;\n",
        "\n",
        "        traverse(root.right);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "597c3469",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1457 lang=java\n",
        " *\n",
        " * [1457] 二叉树中的伪回文路径\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        " /**\n",
        " 总所周知 n & (n - 1) 可以用来消除最后一个1\n",
        "举例: 以下都是二进制表示\n",
        "假设 n = 100100 那么 n - 1 = 100011 因为100 - 1 = 011\n",
        "那么 n & (n - 1) = 100000 所以这是去掉了最后的一个1\n",
        "\n",
        "因为异或的性质是 位相同异或为0 位不同异或为1 例如 1 ^ 1 = 0 ; 1 ^ 0 =1\n",
        "然后比如 5 二进制为 101 , 依据异或的性质 101 ^ 101 = 0\n",
        "所以异或的性质就是两个相同的数 异或为0\n",
        "\n",
        "然后回文串只会有两种情况 要么 1221 要么 121 所以异或后，要么是0 要么是个数为奇数的数字。\n",
        "然后因为题目取值范围是1～9 所以可以用每一位代表一个数字。\n",
        "比如5写成100000，这样哪怕是 121 这种情况 也只会剩下2 也就是100\n",
        "这样我们就可以根据 n & (n - 1) 来消除1 所以判断是否为回文数字的条件就变成了\n",
        "(n == 0 || (n & (n - 1)) == 0)\n",
        "\n",
        " \n",
        "  */\n",
        "class Solution {\n",
        "    int ans = 0;\n",
        "    public int pseudoPalindromicPaths (TreeNode root) {\n",
        "        if (root == null) return 0;\n",
        "        int nums = 0;\n",
        "        dfs(root, nums);\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public void dfs(TreeNode root, int temp) {\n",
        "        int n = temp ^ (1 << root.val);\n",
        "        if (root.left == null && root.right == null) {\n",
        "            if (n == 0 || (n & (n - 1)) == 0) {\n",
        "                ++ans;\n",
        "            }\n",
        "            return;\n",
        "        }\n",
        "        if (root.left != null) {\n",
        "            dfs(root.left, n);\n",
        "        }\n",
        "        if (root.right != null) {\n",
        "            dfs(root.right, n);\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a7281fe8",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=22 lang=java\n",
        " *\n",
        " * [22] 括号生成\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<String> generateParenthesis(int n) {\n",
        "        List<String> ans = new ArrayList<String>();\n",
        "        backtrack(ans,new StringBuffer(),0,0,n);\n",
        "        return ans;\n",
        "    }\n",
        "\n",
        "    public void backtrack(List<String> ans,StringBuffer cur,int open,int close,int max){\n",
        "        if(cur.length() == max *2){\n",
        "            ans.add(cur.toString());\n",
        "            return;\n",
        "        }\n",
        "        if(open < max){\n",
        "            cur.append('(');\n",
        "            backtrack(ans,cur,open+1,close,max);\n",
        "            cur.deleteCharAt(cur.length() -1);\n",
        "        }\n",
        "        if(close < open){\n",
        "            cur.append(')');\n",
        "            backtrack(ans,cur,open,close +1,max);\n",
        "            cur.deleteCharAt(cur.length() -1);\n",
        "        }\n",
        "       \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5da4e8e3",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1657 lang=java\n",
        " *\n",
        " * [1657] 确定两个字符串是否接近\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean closeStrings(String word1, String word2) {\n",
        "        int[] count1 = new int[26],count2=new int[26];\n",
        "        for(char c: word1.toCharArray()){\n",
        "            count1[c -'a']++;\n",
        "        }\n",
        "         for(char c: word2.toCharArray()){\n",
        "            count2[c -'a']++;\n",
        "        }\n",
        "        // 两个 字符串的包含的字符集是一样的\n",
        "        for(int i=0;i<26;i++){\n",
        "            if(count1[i] > 0 && count2[i] ==0 || count1[i] == 0 && count2[i] >0){\n",
        "                return false;\n",
        "            }\n",
        "        }\n",
        "        Arrays.sort(count1);\n",
        "        Arrays.sort(count2);\n",
        "        return Arrays.equals(count1,count2);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e529e99a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=951 lang=java\n",
        " *\n",
        " * [951] 翻转等价二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean flipEquiv(TreeNode root1, TreeNode root2) {\n",
        "        if(root1 == null || root2 == null) {\n",
        "            return root1 == null && root2 == null;\n",
        "        }\n",
        "        return root1.val == root2.val \n",
        "        && (flipEquiv(root1.left, root2.right) || flipEquiv(root1.left, root2.left)) \n",
        "        && (flipEquiv(root1.right, root2.left) || flipEquiv(root1.right, root2.right));\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1e06f459",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=45 lang=java\n",
        " *\n",
        " * [45] 跳跃游戏 II\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int jump(int[] nums) {\n",
        "      int length = nums.length;\n",
        "      // 可跳的最远的位置\n",
        "      int end = 0;\n",
        "      // 最远跳跃长度\n",
        "      int maxPosition = 0;\n",
        "      int steps = 0;\n",
        "      for(int i = 0;i<length-1;i++){\n",
        "          maxPosition = Math.max(maxPosition,i+nums[i]);\n",
        "          if(i == end){\n",
        "              end = maxPosition;\n",
        "              steps++;\n",
        "          }\n",
        "      }\n",
        "      return steps;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7688b6a0",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=189 lang=java\n",
        " *\n",
        " * [189] 轮转数组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public void rotate(int[] nums, int k) {\n",
        "        int n = nums.length;\n",
        "        int[] newArr = new int[n];\n",
        "        for (int i = 0; i < n; ++i) {\n",
        "            newArr[(i + k) % n] = nums[i];\n",
        "        }\n",
        "        System.arraycopy(newArr, 0, nums, 0, n);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "8fa20f84",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=724 lang=java\n",
        " *\n",
        " * [724] 寻找数组的中心下标\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int pivotIndex(int[] nums) {\n",
        "        \n",
        "        int aSum =0;\n",
        "        for(int i=0;i<nums.length;i++){\n",
        "            aSum += nums[i];\n",
        "        }\n",
        "        int lsum =0, left=0;\n",
        "        while(left<nums.length){\n",
        "            if(nums[left] + 2* lsum == aSum){\n",
        "                return left;\n",
        "                \n",
        "            }\n",
        "            lsum += nums[left];\n",
        "            left++;\n",
        "            \n",
        "        }\n",
        "        return -1;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "0a5c4146",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=131 lang=java\n",
        " *\n",
        " * [131] 分割回文串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    boolean[][] f;\n",
        "    List<List<String>> ret = new ArrayList<List<String>>();\n",
        "    List<String> ans = new ArrayList<String>();\n",
        "    int n;\n",
        "\n",
        "    public List<List<String>> partition(String s) {\n",
        "        n = s.length();\n",
        "        f = new boolean[n][n];\n",
        "        for(int i =0;i<n;++i){\n",
        "            Arrays.fill(f[i],true);\n",
        "        }\n",
        "\n",
        "\n",
        "        for(int i=n-1;i>=0;--i){\n",
        "            for(int j = i+1;j< n;++j){\n",
        "                f[i][j] = (s.charAt(i) ==s.charAt(j)) && f[i+1][j-1];\n",
        "            }\n",
        "        }\n",
        "        dfs(s,0);\n",
        "        return ret;\n",
        "    }\n",
        "\n",
        "    public void dfs(String s ,int i){\n",
        "        if(i == n){\n",
        "            ret.add(new ArrayList<String>(ans));\n",
        "            return ;\n",
        "        }\n",
        "\n",
        "        for(int j =i;j<n;++j){\n",
        "            if(f[i][j]){\n",
        "                ans.add(s.substring(i,j+1));\n",
        "                dfs(s,j+1);\n",
        "                ans.remove(ans.size()-1);\n",
        "            }\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "5e430928",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=138 lang=java\n",
        " *\n",
        " * [138] 随机链表的复制\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/*\n",
        "// Definition for a Node.\n",
        "class Node {\n",
        "    int val;\n",
        "    Node next;\n",
        "    Node random;\n",
        "\n",
        "    public Node(int val) {\n",
        "        this.val = val;\n",
        "        this.next = null;\n",
        "        this.random = null;\n",
        "    }\n",
        "}\n",
        "*/\n",
        "\n",
        "class Solution {\n",
        "    Map<Node,Node> cachedNode = new HashMap<Node,Node>();\n",
        "    public Node copyRandomList(Node head) {\n",
        "       if(head == null){\n",
        "           return null;\n",
        "       }\n",
        "       if(!cachedNode.containsKey(head)){\n",
        "           Node headNew = new Node(head.val);\n",
        "           cachedNode.put(head,headNew);\n",
        "           headNew.next = copyRandomList(head.next);\n",
        "           headNew.random = copyRandomList(head.random);\n",
        "       }\n",
        "       return cachedNode.get(head);\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "af56ac46",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2331 lang=java\n",
        " *\n",
        " * [2331] 计算布尔二叉树的值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public boolean evaluateTree(TreeNode root) {\n",
        "        if(root == null) return false;\n",
        "        \n",
        "        if(root.val == 0) return false;\n",
        "        if(root.val == 1) return true;\n",
        "\n",
        "\n",
        "\n",
        "        boolean left = evaluateTree(root.left);\n",
        "        boolean right = evaluateTree(root.right);\n",
        "        if(root.val == 2){\n",
        "            return left || right;\n",
        "        }else if(root.val ==3){\n",
        "            return left && right;\n",
        "        }\n",
        "\n",
        "        return false;\n",
        "\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1b5c3290",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=2336 lang=java\n",
        " *\n",
        " * [2336] 无限集中的最小数字\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class SmallestInfiniteSet {\n",
        "    private int thres;\n",
        "    private TreeSet<Integer> set;\n",
        "\n",
        "    public SmallestInfiniteSet() {\n",
        "        thres =1;\n",
        "        set = new TreeSet<Integer>();\n",
        "    }\n",
        "    \n",
        "    public int popSmallest() {\n",
        "        if(set.isEmpty()){\n",
        "            int ans = thres;\n",
        "            ++thres;\n",
        "            return ans;\n",
        "        }\n",
        "        int ans  =set.pollFirst();\n",
        "        return ans;\n",
        "    }\n",
        "    \n",
        "    public void addBack(int num) {\n",
        "        if(num < thres){\n",
        "            set.add(num);\n",
        "        }\n",
        "    }\n",
        "}\n",
        "\n",
        "/**\n",
        " * Your SmallestInfiniteSet object will be instantiated and called as such:\n",
        " * SmallestInfiniteSet obj = new SmallestInfiniteSet();\n",
        " * int param_1 = obj.popSmallest();\n",
        " * obj.addBack(num);\n",
        " */\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "776be0cb",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=230 lang=java\n",
        " *\n",
        " * [230] 二叉搜索树中第 K 小的元素\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "\n",
        "     //记录结果\n",
        "    int res = 0;\n",
        "    //记录当前元素排名\n",
        "    int rank = 0;\n",
        "    public int kthSmallest(TreeNode root, int k) {\n",
        "\n",
        "        //利用BST 中序遍历特性\n",
        "        traverse(root,k);\n",
        "        return res;\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root,int k ){\n",
        "        if(root == null){\n",
        "            return;\n",
        "        }\n",
        "        traverse(root.left,k);\n",
        "\n",
        "        /* 中序遍历代码位置 */\n",
        "        rank++;\n",
        "        if(k == rank){\n",
        "            res = root.val;\n",
        "            return ;\n",
        "        }\n",
        "        /** */\n",
        "        traverse(root.right,k);\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e4c44fcb",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1268 lang=java\n",
        " *\n",
        " * [1268] 搜索推荐系统\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<String>> suggestedProducts(String[] products, String searchWord) {\n",
        "        Trie trie = new Trie();\n",
        "        Arrays.sort(products);\n",
        "        for(String product:products){\n",
        "            trie.insert(product);\n",
        "        }\n",
        "        List<List<String>> suggestedList = new ArrayList<List<String>>();\n",
        "        Trie node = trie;\n",
        "        int length = searchWord.length();\n",
        "        for(int i=0;i<length;i++){\n",
        "            char c = searchWord.charAt(i);\n",
        "            node = node !=null? node.getChild(c):null;\n",
        "            List<String> list = node!=null?node.getWords():new ArrayList<String>();\n",
        "            suggestedList.add(list);\n",
        "        }\n",
        "        return suggestedList;\n",
        "\n",
        "    }\n",
        "\n",
        "    class Trie{\n",
        "        static final int MAX_COUNT =3;\n",
        "        private List<String> words;\n",
        "        private Map<Character,Trie> children;\n",
        "\n",
        "        public Trie(){\n",
        "            words = new ArrayList<String>();\n",
        "            children = new HashMap<Character,Trie>();\n",
        "        }\n",
        "        public void insert(String word){\n",
        "            Trie node =this;\n",
        "            int length = word.length();\n",
        "            for(int i=0;i<length;i++){\n",
        "                char c = word.charAt(i);\n",
        "                node.children.putIfAbsent(c,new Trie());\n",
        "                node = node.children.get(c);\n",
        "                if(node.words.size() < MAX_COUNT){\n",
        "                    node.words.add(word);\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "\n",
        "        public Trie getChild(char c){\n",
        "            return children.get(c);\n",
        "        }\n",
        "        public List<String> getWords(){\n",
        "            return words;\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c9a70243",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=151 lang=java\n",
        " *\n",
        " * [151] 反转字符串中的单词\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String reverseWords(String s) {\n",
        "        //除去开头和末尾的空白字符\n",
        "        s = s.trim();\n",
        "        //正则匹配连续的空白字符作为分隔符分隔\n",
        "        List<String> wordList = Arrays.asList(s.split(\"\\\\s+\"));\n",
        "        Collections.reverse(wordList);\n",
        "        return String.join(\" \",wordList);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "fdf07686",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=865 lang=java\n",
        " *\n",
        " * [865] 具有所有最深节点的最小子树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "  private int maxDeep = Integer.MIN_VALUE;\n",
        "  private TreeNode result;\n",
        "  public TreeNode subtreeWithAllDeepest(TreeNode root) {\n",
        "    maxDeep(root, 0);\n",
        "    return result;\n",
        "  }\n",
        "  private int maxDeep(TreeNode node, int deep) {\n",
        "    if (node == null) {\n",
        "      return deep;\n",
        "    }\n",
        "    int left = maxDeep(node.left, deep+1);\n",
        "    int right = maxDeep(node.right, deep+1);\n",
        "    /** 后序位置 */\n",
        "    int currentMax = Math.max(left, right);\n",
        "    maxDeep = Math.max(maxDeep, currentMax);\n",
        "    if (left == maxDeep && right == maxDeep) {\n",
        "      result = node;\n",
        "    }\n",
        "    return currentMax;\n",
        "  }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c2e2a1db",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=19 lang=java\n",
        " *\n",
        " * [19] 删除链表的倒数第 N 个结点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "   public ListNode removeNthFromEnd(ListNode head, int n) {\n",
        "\n",
        "         ListNode dummy = new ListNode(-1);\n",
        "         dummy.next = head;\n",
        "         \n",
        "         //抽取方法体ListNode findFromEnd(ListNode head,int k)\n",
        "            ListNode  p1 = dummy;\n",
        "            int len =0;\n",
        "            //以p1 不为null 为循环条件，对比len和n+1的大小，\n",
        "            //将\n",
        "           for(int i=0;i<n+1;i++){\n",
        "               p1 =p1.next;\n",
        "           }\n",
        "\n",
        "            ListNode p2 = dummy;\n",
        "            while(p1!=null){\n",
        "                p1 = p1.next;\n",
        "                p2 = p2.next;\n",
        "            }\n",
        "            // return p2;\n",
        "            //方法体结束\n",
        "            p2.next = p2.next.next;\n",
        "           \n",
        "            return dummy.next;\n",
        "\n",
        "    \n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ba600fc9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=450 lang=java\n",
        " *\n",
        " * [450] 删除二叉搜索树中的节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode deleteNode(TreeNode root, int key) {\n",
        "        if(root == null) return null;\n",
        "        if(root.val == key){\n",
        "            /**\n",
        "             分三种情况\n",
        "             1、 root 是末端节点，直接删除\n",
        "             2、 root 只有一个非空子节点，让孩子接替自己的位置\n",
        "             3、 root 有两个非空子节点，找到左子树中的最大节点，或者右子树的最小节点来接替自己\n",
        "             */\n",
        "             //这两个if 把情况1和2 都正确处理了\n",
        "            if(root.left == null) return root.right;\n",
        "            if(root.right == null) return root.left;\n",
        "            //处理情况3\n",
        "            TreeNode minNode = getMin(root.right);\n",
        "            root.val = minNode.val;\n",
        "            root.right =deleteNode(root.right,minNode.val);\n",
        "\n",
        "        }else if(root.val > key){\n",
        "            root.left = deleteNode(root.left,key);\n",
        "        }else if(root.val < key){\n",
        "            root.right = deleteNode(root.right,key);\n",
        "        } \n",
        "\n",
        "        return root;      \n",
        "    }\n",
        "\n",
        "    TreeNode getMin(TreeNode node){\n",
        "        //BST 最左边的就是最小的\n",
        "        while(node.left!=null) node = node.left;\n",
        "        return node;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "eeba7ce9",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=26 lang=java\n",
        " *\n",
        " * [26] 删除有序数组中的重复项\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int removeDuplicates(int[] nums) {\n",
        "        if(nums.length ==0){\n",
        "            return 0;\n",
        "        }\n",
        "        int slow =0,fast =0;\n",
        "        while(fast<nums.length){\n",
        "            if(nums[fast] !=nums[slow]){\n",
        "                slow ++;\n",
        "                nums[slow] = nums[fast];\n",
        "            }\n",
        "            fast ++;\n",
        "        }\n",
        "        return slow +1;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "eb4ef78f",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1679 lang=java\n",
        " *\n",
        " * [1679] K 和数对的最大数目\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int maxOperations(int[] nums, int k) {\n",
        "        //max 表示可以对数组执行的最大操作\n",
        "        int max = 0;\n",
        "        int len = nums.length;\n",
        "        //升序排序\n",
        "        Arrays.sort(nums);\n",
        "        int i =0,j=len -1;\n",
        "        while(i < j ){\n",
        "            //依次处理3种情况\n",
        "            if(nums[i]+nums[j] == k){\n",
        "                max++;\n",
        "                i++;\n",
        "                j--;\n",
        "            }else if(nums[i] + nums[j] > k){\n",
        "                j--;\n",
        "            }else{\n",
        "                i++;\n",
        "            }\n",
        "        }\n",
        "        return max;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a3fb0aa6",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=136 lang=java\n",
        " *\n",
        " * [136] 只出现一次的数字\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int singleNumber(int[] nums) {\n",
        "        int single = 0;\n",
        "        for(int num:nums){\n",
        "            single ^=num;\n",
        "        }\n",
        "        return single;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cd45e393",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=279 lang=java\n",
        " *\n",
        " * [279] 完全平方数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int numSquares(int n) {\n",
        "      int[] f = new int[n+1];\n",
        "      for(int i =1;i <= n;i++){\n",
        "\n",
        "        int minn = Integer.MAX_VALUE;\n",
        "\n",
        "        for(int j = 1; j*j <= i; j++){\n",
        "           minn =Math.min(minn,f[i-j*j]);\n",
        "        }\n",
        "\n",
        "        f[i] = minn + 1;\n",
        "      }\n",
        "      return f[n];\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1198b4e7",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1379 lang=java\n",
        " *\n",
        " * [1379] 找出克隆二叉树中的相同节点\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode(int x) { val = x; }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "    public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {\n",
        "        if (original == null){\n",
        "            return null;\n",
        "        }\n",
        "        if (original == target){\n",
        "            return cloned;\n",
        "        }\n",
        "        // 递归左子树\n",
        "        TreeNode res = getTargetCopy(original.left,cloned.left,target);\n",
        "        if (res != null){\n",
        "            return res;\n",
        "        }\n",
        "        // 递归右子树\n",
        "        res = getTargetCopy(original.right,cloned.right,target);\n",
        "        if (res != null){\n",
        "            return res;\n",
        "        }\n",
        "        return null;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1cca6005",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=328 lang=java\n",
        " *\n",
        " * [328] 奇偶链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public ListNode oddEvenList(ListNode head) {\n",
        "        if(head == null){\n",
        "            return head;\n",
        "        }\n",
        "        ListNode evenHead = head.next;\n",
        "        ListNode odd = head,even = evenHead;\n",
        "        while(even != null && even.next !=null){\n",
        "            odd.next = even.next;\n",
        "            odd = odd.next;\n",
        "            even.next = odd.next;\n",
        "            even = even.next;\n",
        "        }\n",
        "        odd.next = evenHead;\n",
        "        return head;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3295a5f4",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=763 lang=java\n",
        " *\n",
        " * [763] 划分字母区间\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<Integer> partitionLabels(String s) {\n",
        "        int[] last = new int[26];\n",
        "        int length = s.length();\n",
        "        //每个字母最后一次出现的下标位置\n",
        "        for(int i=0;i<length;i++){\n",
        "            last[s.charAt(i) -'a'] = i;\n",
        "        }\n",
        "        List<Integer> partition = new ArrayList<Integer>();\n",
        "        int start =0,end =0;\n",
        "        for(int i=0;i<length;i++){\n",
        "            end = Math.max(end,last[s.charAt(i) - 'a']);\n",
        "            if(i == end){\n",
        "                // end - start + 1  片段长度\n",
        "                partition.add(end - start +1);\n",
        "                start = end + 1;\n",
        "            }\n",
        "        }\n",
        "        return partition;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7c948e77",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=654 lang=java\n",
        " *\n",
        " * [654] 最大二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public TreeNode constructMaximumBinaryTree(int[] nums) {\n",
        "               \n",
        "               /**\n",
        "               1、在[0，nums.length -1] 找出最大值 作为根节点\n",
        "               2、[0,i-1] [i+1,nums.length-1] \n",
        "               \n",
        "               \n",
        "                */\n",
        "                return build(nums,0,nums.length-1);\n",
        "               \n",
        "               \n",
        "    }\n",
        "\n",
        "\n",
        "    TreeNode build(int[] nums,int lo,int hi){\n",
        "        if(lo > hi){\n",
        "            return null;\n",
        "        }\n",
        "        int maxIdx =-1,maxVal = Integer.MIN_VALUE;\n",
        "        for(int i=lo;i<=hi;i++){\n",
        "            if(nums[i]>maxVal){\n",
        "                maxIdx = i;\n",
        "                maxVal =nums[i];\n",
        "            }\n",
        "        }\n",
        "        TreeNode root =new TreeNode(maxVal);\n",
        "\n",
        "        root.left = build(nums,lo,maxIdx-1);\n",
        "        root.right = build(nums,maxIdx+1,hi);\n",
        "\n",
        "        return root;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7dbadf09",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=515 lang=java\n",
        " *\n",
        " * [515] 在每个树行中找最大值\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    public List<Integer> largestValues(TreeNode root) {\n",
        "        List<Integer> levelMaxs= new ArrayList<>();\n",
        "        if(root == null) return levelMaxs ;\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "        \n",
        "\n",
        "        while(!q.isEmpty()){\n",
        "            int sz = q.size();\n",
        "            // 每层，最初max为 integer 最小值， 不能随意定为0，因为节点数值可能是小于0的值\n",
        "            int max = Integer.MIN_VALUE;\n",
        "            for(int i =0;i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                max = Math.max(cur.val,max);\n",
        "\n",
        "                if(cur.left !=null){\n",
        "                    q.offer(cur.left);\n",
        "                }\n",
        "                if(cur.right!=null){\n",
        "                    q.offer(cur.right);\n",
        "                }\n",
        "\n",
        "            \n",
        "            }\n",
        "\n",
        "            levelMaxs.add(max);\n",
        "        }\n",
        "\n",
        "        return levelMaxs;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "eb021b3d",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=25 lang=java\n",
        " *\n",
        " * [25] K 个一组翻转链表\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for singly-linked list.\n",
        " * public class ListNode {\n",
        " *     int val;\n",
        " *     ListNode next;\n",
        " *     ListNode() {}\n",
        " *     ListNode(int val) { this.val = val; }\n",
        " *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "   // 反转以 a 为头结点的链表\n",
        "ListNode reverse(ListNode a) {\n",
        "    ListNode pre, cur, nxt;\n",
        "    pre = null; cur = a; nxt = a;\n",
        "    while (cur != null) {\n",
        "        nxt = cur.next;\n",
        "        // 逐个结点反转\n",
        "        cur.next = pre;\n",
        "        // 更新指针位置\n",
        "        pre = cur;\n",
        "        cur = nxt;\n",
        "    }\n",
        "    // 返回反转后的头结点\n",
        "    return pre;\n",
        "}\n",
        "\n",
        "/** 反转区间 [a, b) 的元素，注意是左闭右开 */\n",
        "ListNode reverse(ListNode a, ListNode b) {\n",
        "    ListNode pre, cur, nxt;\n",
        "    pre = null; cur = a; nxt = a;\n",
        "    // while 终止的条件改一下就行了\n",
        "    while (cur != b) {\n",
        "        nxt = cur.next;\n",
        "        cur.next = pre;\n",
        "        pre = cur;\n",
        "        cur = nxt;\n",
        "    }\n",
        "    // 返回反转后的头结点\n",
        "    return pre;\n",
        "}\n",
        "\n",
        "ListNode reverseKGroup(ListNode head, int k) {\n",
        "    if (head == null) return null;\n",
        "    // 区间 [a, b) 包含 k 个待反转元素\n",
        "    ListNode a, b;\n",
        "    a = b = head;\n",
        "    for (int i = 0; i < k; i++) {\n",
        "        // 不足 k 个，不需要反转，base case\n",
        "        if (b == null) return head;\n",
        "        b = b.next;\n",
        "    }\n",
        "    // 反转前 k 个元素\n",
        "    ListNode newHead = reverse(a, b);\n",
        "    // 递归反转后续链表并连接起来\n",
        "    a.next = reverseKGroup(b, k);\n",
        "    return newHead;\n",
        "}\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1e089754",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=404 lang=java\n",
        " *\n",
        " * [404] 左叶子之和\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    int sum =0;\n",
        "    public int sumOfLeftLeaves(TreeNode root) {\n",
        "        return traverse(root);\n",
        "        \n",
        "\n",
        "    }\n",
        "\n",
        "    int traverse(TreeNode root){\n",
        "        if(root == null) return 0 ;\n",
        "        int res = 0;\n",
        "        if(root.left !=null && root.left.left == null\n",
        "                && root.left.right == null){\n",
        "                    res +=root.left.val;\n",
        "                }\n",
        "        return traverse(root.left)+traverse(root.right)+res;\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "61eee950",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=55 lang=java\n",
        " *\n",
        " * [55] 跳跃游戏\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public boolean canJump(int[] nums) {\n",
        "        int n = nums.length;\n",
        "        int rightmost =0;\n",
        "        for(int i=0;i<n;++i){\n",
        "            // 位置可达\n",
        "            if( i <= rightmost ){\n",
        "                rightmost = Math.max(rightmost,i+nums[i]);\n",
        "                //尾部可达\n",
        "                if(rightmost >= n-1){\n",
        "                    return true;\n",
        "                }\n",
        "            }\n",
        "        }\n",
        "        return false;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ced7dede",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1137 lang=java\n",
        " *\n",
        " * [1137] 第 N 个泰波那契数\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public int tribonacci(int n) {\n",
        "        if(n==0){\n",
        "            return 0;\n",
        "        }\n",
        "        if(n<=2){\n",
        "            return 1;\n",
        "        }\n",
        "        int p =0,q=0,r=1,s=1;\n",
        "        for(int i=3;i<=n;i++){\n",
        "            p = q;\n",
        "            q = r;\n",
        "            r =s;\n",
        "            s = p + q + r;\n",
        "        }\n",
        "        return s;\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "c03d6399",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=99 lang=java\n",
        " *\n",
        " * [99] 恢复二叉搜索树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "\n",
        "class Solution {\n",
        "     TreeNode x = null;\n",
        "     TreeNode y = null;\n",
        "     TreeNode pre = null;\n",
        "    public void recoverTree(TreeNode root) {\n",
        "        dfs(root);\n",
        "        //System.out.println(x.val);\n",
        "         //System.out.println(y.val);\n",
        "        //如果x和y都不为空，说明二叉搜索树出现错误的节点，将其交换\n",
        "        if(x!=null && y!=null) {\n",
        "            int tmp = x.val;\n",
        "            x.val = y.val;\n",
        "            y.val = tmp;\n",
        "        }\n",
        "    }\n",
        "\t\n",
        "    //中序遍历二叉树，并比较上一个节点(pre)和当前节点的值，如果pre的值大于当前节点值，则记录下这两个节点\n",
        "    public void dfs(TreeNode node) {\n",
        "        if(node==null) {\n",
        "            return;\n",
        "        }\n",
        "        dfs(node.left);\n",
        "        \n",
        "        /**  中序位置 */\n",
        "        if(pre!=null &&(pre.val>node.val)) {\n",
        "            if(y ==null ){\n",
        "                 y = pre;    \n",
        "            }\n",
        "            x = node;\n",
        "        }\n",
        "        \n",
        "        //pre 指针指向当前节点\n",
        "        pre = node;\n",
        "        dfs(node.right);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "3db1e236",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=49 lang=java\n",
        " *\n",
        " * [49] 字母异位词分组\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public List<List<String>> groupAnagrams(String[] strs) {\n",
        "        return new ArrayList<>(Arrays.stream(strs).collect(Collectors.groupingBy(str -> {  \n",
        "            char[] array =str.toCharArray();\n",
        "            Arrays.sort(array);\n",
        "            return new String(array);\n",
        "        })).values());\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e707b954",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=199 lang=java\n",
        " *\n",
        " * [199] 二叉树的右视图\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "/**\n",
        " * Definition for a binary tree node.\n",
        " * public class TreeNode {\n",
        " *     int val;\n",
        " *     TreeNode left;\n",
        " *     TreeNode right;\n",
        " *     TreeNode() {}\n",
        " *     TreeNode(int val) { this.val = val; }\n",
        " *     TreeNode(int val, TreeNode left, TreeNode right) {\n",
        " *         this.val = val;\n",
        " *         this.left = left;\n",
        " *         this.right = right;\n",
        " *     }\n",
        " * }\n",
        " */\n",
        "class Solution {\n",
        "    List<Integer> res = new LinkedList<>();\n",
        "    public List<Integer> rightSideView(TreeNode root) {\n",
        "\n",
        "        traverse(root);\n",
        "        return res;\n",
        "\n",
        "    }\n",
        "\n",
        "    void traverse(TreeNode root){\n",
        "        if(root == null) return ;\n",
        "\n",
        "        Queue<TreeNode> q = new LinkedList<>();\n",
        "        q.offer(root);\n",
        "\n",
        "        while(!q.isEmpty()){\n",
        "            int sz = q.size();\n",
        "            for(int i=0;i<sz;i++){\n",
        "                TreeNode cur = q.poll();\n",
        "                if(cur.left !=null){\n",
        "                    q.offer(cur.left);\n",
        "                }\n",
        "                if(cur.right!=null){\n",
        "                    q.offer(cur.right);\n",
        "                }\n",
        "                if(i == sz -1){\n",
        "                    res.add(cur.val);\n",
        "                }\n",
        "            }\n",
        "\n",
        "        }\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "ff8f8b5a",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=75 lang=java\n",
        " *\n",
        " * [75] 颜色分类\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public void sortColors(int[] nums) {\n",
        "        int n = nums.length;\n",
        "        int ptr =0;\n",
        "        for(int i =0;i<n;i++){\n",
        "            if(nums[i] == 0){\n",
        "                int temp = nums[i];\n",
        "                nums[i] = nums[ptr];\n",
        "                nums[ptr] = temp;\n",
        "                ptr++;\n",
        "            }\n",
        "        }\n",
        "\n",
        "        for(int i=ptr;i<n;i++){\n",
        "            if(nums[i] == 1){\n",
        "                int temp = nums[i];\n",
        "                nums[i] = nums[ptr];\n",
        "                nums[ptr] = temp;\n",
        "                ptr++;\n",
        "            }\n",
        "        }\n",
        "\n",
        "\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "6162dc32",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=1361 lang=java\n",
        " *\n",
        " * [1361] 验证二叉树\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    // 并查集用的集合列表\n",
        "    List<Integer> p = new ArrayList<>();\n",
        "    // 用于统计不相交的连通分支个数\n",
        "    int cnt;\n",
        "    public boolean validateBinaryTreeNodes(int n, int[] leftChild, int[] rightChild) {\n",
        "        // 用于标记各个孩子的父节点\n",
        "        int[] father = new int[n];\n",
        "        // 初始化\n",
        "        Arrays.fill(father, -1);\n",
        "        // 初始化并查集集合状态\n",
        "        for(int i = 0; i < n; i++) p.add(i);\n",
        "        // 初始化分支数\n",
        "        cnt = n;\n",
        "        // 遍历所有节点\n",
        "        for(int i = 0; i < n; i++) {\n",
        "            // 如果节点存在两个孩子，而且两个孩子相同，那么显然是错误的二叉树\n",
        "            if(leftChild[i] == rightChild[i] && leftChild[i] != -1) return false;\n",
        "            // 合并两个孩子\n",
        "            if(!merge(father, i, leftChild[i]) || !merge(father, i, rightChild[i])) return false;\n",
        "        }\n",
        "\n",
        "        // 如果最后所有的节点组成一个连通分支，才是一棵树\n",
        "        if(cnt == 1) return true;\n",
        "        return false;\n",
        "\n",
        "    }\n",
        "    // 和并父亲和孩子节点，并判断逻辑\n",
        "    private boolean merge(int[] father, int f, int c) {\n",
        "        // 孩子是空的，直接返回\n",
        "        if(c == -1) return true;\n",
        "        // 孩子之前有爸爸了，就是错的\n",
        "        if(father[c] != -1) return false;\n",
        "        // 并查集查找两个集合的根\n",
        "        int a = find(f), b = find(c);\n",
        "        // 如果孩子和父亲已经存在于一个集合中，那么说明会产生环，返回错误\n",
        "        if(a == b) return false;\n",
        "        // 合并两个集合\n",
        "        p.set(a, b);\n",
        "        // 标记孩子的父亲是谁\n",
        "        father[c] = f;\n",
        "        // 连通分支数减一\n",
        "        cnt --;\n",
        "        return true;\n",
        "    }\n",
        "    // 并查集通用方法，找集合的根元素\n",
        "    private int find(int x) {\n",
        "        if(p.get(x) != x) {\n",
        "            p.set(x, find(p.get(x)));\n",
        "        }\n",
        "        return p.get(x);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4d0f37b3",
      "metadata": {},
      "outputs": [],
      "source": [
        "/*\n",
        " * @lc app=leetcode.cn id=5 lang=java\n",
        " *\n",
        " * [5] 最长回文子串\n",
        " */\n",
        "\n",
        "// @lc code=start\n",
        "class Solution {\n",
        "    public String longestPalindrome(String s) {\n",
        "        String res =\"\";\n",
        "          for(int i=0;i<s.length();i++){\n",
        "              String s1 = palindrome(s,i,i);\n",
        "              String s2 =palindrome(s,i,i+1);\n",
        "\n",
        "              res = res.length()>s1.length()?res:s1;\n",
        "              res = res.length() >s2.length()? res:s2;\n",
        "\n",
        "          }\n",
        "          return res;  \n",
        "    }\n",
        "     public  String palindrome(String s,int l,int r){\n",
        "       // 为什么 l >=0 而不是 l > 0\n",
        "        while(l>=0\n",
        "           &&r<s.length()\n",
        "           &&s.charAt(l) ==s.charAt(r)){\n",
        "               l--;\n",
        "               r++;\n",
        "           }\n",
        "        return s.substring(l+1,r);\n",
        "    }\n",
        "}\n",
        "// @lc code=end\n",
        "\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Java",
      "language": "java",
      "name": "java"
    },
    "language_info": {
      "codemirror_mode": "java",
      "file_extension": ".java",
      "mimetype": "text/x-java-source",
      "name": "java"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}